0
0
Pythonprogramming~10 mins

String formatting using f-strings in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the name using an f-string.

Python
name = "Alice"
print(f"Hello, [1]!")
Drag options to blanks, or click blank then click option'
Aname()
B"name"
CName
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the variable name inside the braces.
Using a function call instead of the variable.
Capitalizing the variable name incorrectly.
2fill in blank
medium

Complete the code to format the number with two decimal places using an f-string.

Python
price = 9.8765
print(f"Price: $[1]")
Drag options to blanks, or click blank then click option'
Aprice:.1f
Bprice:.3f
Cprice:.2f
Dprice:.0f
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number of decimal places.
Forgetting the colon before the format specifier.
Using quotes inside the braces.
3fill in blank
hard

Fix the error in the f-string to correctly display the age variable.

Python
age = 30
print(f"Age: [1] years")
Drag options to blanks, or click blank then click option'
Aage
Bage)
C(age
D"age"
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses inside the braces.
Putting quotes around the variable name.
Leaving unmatched parentheses.
4fill in blank
hard

Fill both blanks to create an f-string that shows the product name and its price with two decimals.

Python
product = "Book"
price = 12.5
print(f"[1] costs $[2]")
Drag options to blanks, or click blank then click option'
Aproduct
Bprice
Cprice:.2f
D"product"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the variable names.
Not formatting the price to two decimals.
Swapping the variables in the blanks.
5fill in blank
hard

Fill all three blanks to create an f-string that shows the user's name in uppercase, their age, and a message.

Python
user = "emma"
age = 25
print(f"User: [1], Age: [2], Message: [3]")
Drag options to blanks, or click blank then click option'
Auser.upper()
Bage
C"Welcome!"
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling the upper() method.
Putting the message without quotes.
Using the variable user without upper().