A Python f-string is a string that starts with f and lets you put variables and expressions inside curly brackets: f"Total: ${total}". After a colon inside the brackets you add a format code, such as .2f to show two decimals or , to add thousands separators. It is the shortest and most readable way to print a variable in a string.
How do you print a variable in a string?
item = "latte" quantity = 2 price = 4.25 print(f"{quantity} {item}: ${quantity * price}")
The output is 2 latte: $8.5. You can put a variable or an expression like quantity * price between the brackets. Money needs two decimals, and the next section fixes that.
Without an f-string, the same line is harder to write and read:
item = "latte" quantity = 2 price = 4.25 print(str(quantity) + " " + item + ": $" + str(quantity * price))
How do you format a number to 2 decimals in Python?
Add :.2f after the value. It means "show 2 digits after the decimal point."
average = 4.0833333 print(f"Average order: ${average:.2f}") print(f"Rounded: ${average:.0f}")
Average order: $4.08 Rounded: $4
The value is rounded only in the text. The variable itself does not change.
How do you show a percentage?
rate = 0.1834 print(f"Conversion rate: {rate:.1%}")
:.1% multiplies by 100, shows 1 decimal and adds the % sign: Conversion rate: 18.3%.
How do you add a thousands separator?
revenue = 1284500.5 print(f"Monthly revenue: ${revenue:,.2f}")
The output is Monthly revenue: $1,284,500.50. The code ,.2f combines the thousands separator with two decimals. Without decimals, {revenue:,} prints 1,284,500.5.
How do you align columns?
To print a receipt, set a width and an alignment:
lines = [("tea", 3, 3.75), ("cookie", 1, 2.00), ("lemonade", 2, 6.50)] for item, quantity, amount in lines: print(f"{item:<10}{quantity:>3}{amount:>8.2f}")
tea 3 3.75 cookie 1 2.00 lemonade 2 6.50
<10: left-align in 10 characters.>3: right-align in 3 characters.>8.2f: right-align in 8 characters with 2 decimals.^10: center.
How do you add leading zeros?
table = 7 print(f"Order-{table:03}")
The output is Order-007. This is useful for order numbers and invoice numbers.
How do you format a date?
The same colon syntax works for dates:
from datetime import date today = date(2026, 11, 30) print(f"Receipt date: {today:%B %d, %Y}") print(f"Receipt date: {today:%Y-%m-%d}")
Receipt date: November 30, 2026 Receipt date: 2026-11-30
%B is the month name, %d the day and %Y the four-digit year.
Is there a shortcut for debugging?
total = 29.75 print(f"{total=}")
This prints the name and the value together: total=29.75. It is a fast way to check a value while you debug.
Quick reference
| Code | Example | Result |
|---|---|---|
.2f | f"{3.14159:.2f}" | 3.14 |
.1% | f"{0.256:.1%}" | 25.6% |
, | f"{12000:,}" | 12,000 |
,.2f | f"{1234.5:,.2f}" | 1,234.50 |
>8 | f"{45:>8}" | 45 |
03 | f"{7:03}" | 007 |
Common mistakes
- Forgetting the `f`:
"Total: {total}"prints the brackets as they are. - Quote clashes: inside a double-quoted f-string, use single quotes for dictionary keys:
f"{menu['tea']}". Python 3.12 and later also accept the same quote type, but older versions raise an error. - Using `.2f` on a string: the format code needs a number. A string like
"4.25"raises aValueError, so convert it withfloat()first. - Printing curly brackets: double them.
f"{{total}}"prints{total}.
What to read next
- Practice formatting in the lesson [Formatting](/learn/python/formatting).
- Build a full receipt in the project [Café receipt](/projects/cafe-receipt).
- See where numbers come from in [How to read a CSV file in Python](/blog/python-read-csv-file).
- #python
- #strings
- #f-strings
- #formatting