Course outline · 0% complete

0/32 lessons0%

Course overview →

f-strings: Building Text

lesson 2-3 · ~10 min · 6/32

The f-string

Programs spend a surprising share of their time producing text for humans: receipts, log lines, error messages, reports. Every language needs a way to drop computed values into a sentence, and the f-string is Python's standard tool for it.

An f-string is a string with a lowercase f in front of the quotes. Inside it, anything wrapped in curly braces {} is evaluated and its result is dropped into the text:

name = "Ada"
age = 36
print(f"{name} is {age}")        # Ada is 36
print(f"Next year: {age + 1}")   # full expressions work

This replaces gluing pieces together with +, which forces you to convert every number with str() first. f-strings convert for you.

Values and expressions inside the braces

The first two lines drop values into a sentence, and the last line adds a format spec: after a colon inside the braces, .2f means display the number as a float with exactly two decimal places.

name = "Ada"
age = 36
print(f"{name} is {age}")
print(f"Next year: {age + 1}")
price = 19.999
print(f"Price: {price:.2f}")

Output

Ada is 36
Next year: 37
Price: 20.00

The second line shows that braces hold real expressions, not just names, so age + 1 is computed on the spot and only the result appears in the text. The third line rounds 19.999 to 20.00, which demonstrates two things at once: the spec controls how many decimals are shown, and it rounds rather than truncating. Note also that age needed no conversion, since an f-string turns each value into text for you.

print(f"{7 / 2:.1f}") outputs 3.5.

Two steps happen inside the braces. The expression 7 / 2 runs first and produces the float 3.5, then the spec .1f formats that result to exactly one decimal place. Asking for :.2f instead would display the same number as 3.50, since the spec pads to the requested width rather than reporting only the digits that happen to be present.

A single f-string can hold several substitutions, including one that does arithmetic. The total is computed inside the braces rather than typed as a literal, so changing price or qty keeps the sentence correct.

item = "notebook"
price = 4.5
qty = 3
print(f"{qty} x {item} costs ${qty * price:.2f}")

Output

3 x notebook costs $13.50

The third pair of braces contains the whole expression qty * price followed by the spec, so multiplication runs first and formatting applies to its result. That is what turns 13.5 into 13.50, the form prices are normally written in. The dollar sign sits outside the braces because it is literal text, not something to be evaluated.

The same pattern produces a weather line, this time rounding to a single decimal place.

city = "Lisbon"
temp = 21.456
print(f"{city}: {temp:.1f} degrees")

Output

Lisbon: 21.5 degrees

The spec .1f rounds 21.456 up to 21.5 for display, while temp itself keeps its full precision. That separation is the useful part of formatting: the value stays exact for any further calculation, and only the human-facing text is shortened. Everything outside the braces, including the colon and the word degrees, is printed literally.