Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Create a More Complex Function | What is a Function?
Python Functions: From Zero to Hero

book
Challenge: Create a More Complex Function

Python 3.6 has a way to format strings that allows you to use built-in expressions inside string constants. This approach is called f-strings. Let's look at the examples.

string = 'world'
print(f'Hello, {string}')
12
string = 'world' print(f'Hello, {string}')
copy

Let's look at the syntax. All text you want to print must be enclosed in quotation marks preceded by an f, and any variables you want to print within that text must be enclosed in curly braces. You will also need to use f-strings in the task.

Opgave

Swipe to start coding

Let's complicate the task. You have to create a function named function(), and create a variable inside a function as capital, assign a value like London; use f-strings and print() function to display the following text: London is the capital and largest city of England and the United Kingdom

Løsning

def function():
capital = 'London'
print(f'{capital} is the capital and largest city of England and the United Kingdom')

function()

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
# Write your code below
def ___():
capital = ___
print(___'{___} ___')


# Testing
function()

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt