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.
| Technique | Syntax | Result | When to use it |
|---|---|---|---|
| simple f-string | f"User: {name}" | "User: Ada" | 95% of daily cases |
| Decimal precision | f"{price:.2f}" | "19.90" | Currency, metrics, floats |
| Thousands separator | f"{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
# 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 -> DoneInterpolation and mini-format language (:.Nf, :,, =)
# 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|# Esempio 4: graffe letterali con doppio {{ }} (es. template JSON)
service = "auth"
json_snippet = f'{{"service": "{service}"}}'
print(json_snippet) # {"service": "auth"}Quick check
What does this fragment print with a format spec f-string?
tax = 0.2
print(f"{tax:.3f}")What does the instruction print('A', 'B', sep='|', end='!') produce?
Test yourself
Step 1 — Basic interpolation with f-string
Construct a human-readable formatted string by combining name and age.
Given name = 'Ada' and age = 30, use an f-string to assign line to the string 'Ada: 30'. Currency line.
Show hint
Write f"{name}: {age}" remembering the letter f before the quotation marks.
Solution available after 3 attempts
Step 2 — Fixed-point rounding in print
Use :.2f to format exactly 2 decimal places and print the badge.
Starting from pi = 3.14159, create badge = f'pi={pi:.2f}' (to 2 decimal places), print it with print(badge) and evaluate to badge.
Show hint
Insert :.2f inside the braces after pi: {pi:.2f}.
Solution available after 3 attempts
Step 3 — Receipt formatted with currency
Combine static text, numeric variable and f-string into a consistent format.
Given price = 19.99, use an f-string to construct receipt_line with the exact text 'Price: 19.99 EUR'. Currency receipt_line.
Show hint
Construct f"Price: {price:.2f} EUR" (or {price}) and evaluate receipt_line.
Solution available after 3 attempts