BBampaa

Lessons › Python

Python lesson

f-strings

Without the f, you'd see the curly brackets themselves. With it, Python fills them in. It's the easiest way to write a sentence with numbers in it.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

An f-string is text with an f in front. Anything inside {curly brackets} is replaced by its value:

product = "Earbuds"
total = 140
print(f"{product} made {total}")

Output:

Earbuds made 140

Key points

f"..."Put f straight before the opening quote.
{total}Curly brackets: the value of total goes here.
f"{a} and {b}"Use as many as you like in one string.

Put together: print(f"{store}: {total}") shows something like Lagos: 264.

Practice

Try each one in your head first, then open the answer.

1. Build a line that shows the product and its total, like Earbuds: 140.
print(f"{product}: {total}")
2. Build a line that stores the text Lagos orders in msg, using the store variable.
msg = f"{store} orders"
3. Print a line like Charger costs 15, using the product and price variables.
product = "Charger"
price = 15
print(f"{product} costs {price}")
4. Print a line like Total: 140, where the number is price * qty.
price = 35
qty = 4
print(f"Total: {price * qty}")
5. Print a line like Ada spent 70, using name and spent.
name = "Ada"
spent = 70
print(f"{name} spent {spent}")

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed