Skip to main content
Module 1 · Lesson 4 of 44/36 in the course~14 min
Module lessons (4/4)

print() and f-strings

Printing values to the console with print() and formatting text with f-strings are the first daily actions of any Python script. f-strings (introduced in Python 3.6) evaluate real expressions directly inside {...}, offering both readability and great performance compared to .format() and %.

The mental model: interpolation + format mini-language

Each placeholder inside an f-string has two optional parts after the expression name: {valore : format_spec}. Everything before the colon is Python code; everything that follows instructs Python how to align or round that string value.

TechniqueSyntaxResultWhen to use it
simple f-stringf"User: {name}""User: Ada"95% of daily cases
Decimal precisionf"{price:.2f}""19.90"Currency, metrics, floats
Thousands separatorf"{users:,}""1,250,000"Reports and dashboards
Fast Debugging (3.8+)f"{score=}""score=95"Log and quick inspection

Check print() with sep and end

Python
# Esempio 1: sep ed end cambiano il separatore tra argomenti e il finale di riga
print("2026", "04", "15", sep="-")      # 2026-04-15
print("Step 1", end=" -> ")             # nessun newline, prosegue sulla stessa riga
print("Done")                           # Step 1 -> Done

Interpolation and mini-format language (:.Nf, :,, =)

Python
# Esempio 2: espressioni e precisione fixed-point dentro le f-string
unit_price = 12.5
quantity = 3
print(f"Total: {unit_price * quantity:.2f} EUR")   # Total: 37.50 EUR

# Esempio 3: debug rapido e allineamento in colonne fisse
total_users = 1_250_000
item_code = "API"
print(f"{total_users=:,}")                         # total_users=1,250,000
print(f"|{item_code:<6}|{quantity:>4}|")           # |API   |   3|
Python
# Esempio 4: graffe letterali con doppio {{ }} (es. template JSON)
service = "auth"
json_snippet = f'{{"service": "{service}"}}'
print(json_snippet)   # {"service": "auth"}

Quick check

Python Quiz#python.m1.l4.e4
Ready

What does this fragment print with a format spec f-string?

Python
tax = 0.2
print(f"{tax:.3f}")
Answer options
Python Quiz#python.m1.l4.e5
Ready

What does the instruction print('A', 'B', sep='|', end='!') produce?

Answer options

Test yourself

Warm-Up Stage

Step 1 — Basic interpolation with f-string

Construct a human-readable formatted string by combining name and age.

Exercise#python.m1.l4.e1
Attempts: 0Loading…

Given name = 'Ada' and age = 30, use an f-string to assign line to the string 'Ada: 30'. Currency line.

Loading editor…
Show hint

Write f"{name}: {age}" remembering the letter f before the quotation marks.

Solution available after 3 attempts

Practice Stage

Step 2 — Fixed-point rounding in print

Use :.2f to format exactly 2 decimal places and print the badge.

Exercise#python.m1.l4.e2
Attempts: 0Loading…

Starting from pi = 3.14159, create badge = f'pi={pi:.2f}' (to 2 decimal places), print it with print(badge) and evaluate to badge.

Loading editor…
Show hint

Insert :.2f inside the braces after pi: {pi:.2f}.

Solution available after 3 attempts

Final Challenge

Step 3 — Receipt formatted with currency

Combine static text, numeric variable and f-string into a consistent format.

Exercise#python.m1.l4.e3
Attempts: 0Loading…

Given price = 19.99, use an f-string to construct receipt_line with the exact text 'Price: 19.99 EUR'. Currency receipt_line.

Loading editor…
Show hint

Construct f"Price: {price:.2f} EUR" (or {price}) and evaluate receipt_line.

Solution available after 3 attempts