Course outline · 0% complete

0/27 lessons0%

Course overview →

f-strings: dropping values into text

lesson 3-4 · ~10 min · 11/27

Building sentences from values

Almost every program ends by turning its variables back into text a human reads: "Your total is $27.50", "3 new messages", "Level 7 complete". Doing that with concatenation is clumsy — + only joins strings (lesson 2-3), so every number needs a str(...) conversion and the line fills up with quotes and plus signs. Python added f-strings to fix exactly this.

Put an f immediately before the opening quote, and inside the string, anything wrapped in curly braces { } is evaluated as Python and its value is inserted into the text at that spot:

name = "Ada"
age = 36
print(f"{name} is {age} years old.")

Output: Ada is 36 years old. The braces can hold any expression, not just a variable name — {age + 10} computes first, then the result lands in the string. No str(...) needed; f-strings convert numbers to text for you.

The f stands for formatted. Without it, the braces are just ordinary characters and would print literally.

Code exercise · python

Run it. Then change the two variables and run again — the sentences update themselves.

Quiz

What does print(f"{2 + 3} apples") output?

Code exercise · python

Your turn. Using the two variables provided and ONE print with an f-string, output exactly: It is 21 degrees in Lisbon today.