Exercises
f-Strings
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Rewrite the concatenation as a single f-string.
Python
name = "Ada"
age = 36
print("Name: " + name + ", age " + str(age))Put an f before the quotes and the names in braces.
name = "Ada"
age = 36
print(f"Name: {name}, age {age}")Exercise 2Passed
Print the total to exactly two decimal places.
Python
price = 19.99
count = 3
# print 59.97
A format spec of .2f goes after a colon inside the braces.
price = 19.99
count = 3
print(f"{price * count:.2f}")Exercise 3Passed
Print the share as a percentage with one decimal place.
Python
share = 0.3421
# print 34.2%
The % format spec multiplies by 100 and adds the sign for you.
share = 0.3421
print(f"{share:.1%}")