f-Strings · Examples
Beginner

f-Strings Examples

Short, runnable programs that show python f-strings in action. Read the explanation, press Run, then change the code and try your own ideas. New to the topic? Start with the Python f-Strings lesson.

1A tidy price table

Width and alignment make columns line up. The product name is left-aligned and the price is right-aligned with two decimals.

A tidy price table
products = {"Tea": 3.5, "Coffee": 12, "Chocolate cake": 4.25}

print(f"{'Item':<16}{'Price':>8}")
print("-" * 24)
for item, price in products.items():
    print(f"{item:<16}{price:>8.2f}")
Expected outputItem               Price
------------------------
Tea                 3.50
Coffee             12.00
Chocolate cake      4.25

2Percentages and thousands separators

A spec such as .1% multiplies by 100 and adds the percent sign, so you pass a ratio, not a percentage.

Percentages and thousands separators
visitors = 1250000
signups = 48200
rate = signups / visitors

print(f"Visitors: {visitors:,}")
print(f"Sign-ups: {signups:,}")
print(f"Conversion: {rate:.2%}")
Expected outputVisitors: 1,250,000
Sign-ups: 48,200
Conversion: 3.86%

3Debugging with the = specifier

The = form prints the expression text for you, which is faster than writing labels by hand.

Debugging with the = specifier
cart = [12.5, 8, 4.5]
subtotal = sum(cart)
discount = 0.1

print(f"{len(cart)=}")
print(f"{subtotal=}")
print(f"{subtotal * (1 - discount)=:.2f}")
Expected outputlen(cart)=3
subtotal=25.0
subtotal * (1 - discount)=22.50

4Numbers in binary, hex and with zero padding

Type letters change the number system: b binary, x hex, o octal. Padding with zeros is common for IDs and file names.

Numbers in binary, hex and with zero padding
for number in [5, 42, 255]:
    print(f"{number:>3} -> bin {number:08b}  hex {number:02x}  id #{number:05d}")
Expected output  5 -> bin 00000101  hex 05  id #00005
 42 -> bin 00101010  hex 2a  id #00042
255 -> bin 11111111  hex ff  id #00255

5Formatting a fixed date

Date objects understand strftime codes in the spec. A fixed date keeps the output the same every run.

Formatting a fixed date
from datetime import date

invoice_date = date(2026, 11, 5)
print(f"Invoice date: {invoice_date:%d/%m/%Y}")
print(f"Long form: {invoice_date:%B %d, %Y}")
print(f"Weekday: {invoice_date:%A}")
print(f"Month number: {invoice_date:%m}")
Expected outputInvoice date: 05/11/2026
Long form: November 05, 2026
Weekday: Thursday
Month number: 11

6Leaderboard with a computed column width

The width comes from the longest name, so the table adapts to any data.

Leaderboard with a computed column width
scores = {"Asha": 92.5, "Benjamin": 88, "Li": 95.25}
width = max(len(name) for name in scores)

ranked = sorted(scores.items(), key=lambda pair: pair[1], reverse=True)
for place, (name, score) in enumerate(ranked, start=1):
    print(f"{place}. {name:<{width}}  {score:>6.2f}")
Expected output1. Li         95.25
2. Asha       92.50
3. Benjamin   88.00

Practise next

Python f-Strings exercises → Back to the lesson

Python Numbers examplesPython Strings examplesPython String Methods examplesPython Input and Output examples