Python Tutorial › Strings › String Formatting & f-strings

Python String Formatting & f-strings

BeginnerLesson 17 of 4710 min read8 runnable examples

Formatting turns values into neat, readable text: prices with two decimals, columns that line up, percentages, dates. f-strings are the modern way to do it, and the format codes you learn here also work with str.format().

Advertisement
In this lesson
  • Put values into text with f-strings
  • Control decimals, width and alignment
  • Format numbers, percentages and dates
  • Recognise format() and % formatting in older code

f-string basics

Put an f before the opening quote and write any expression inside { }.

Python
name = "Asha"tests, passed = 40, 37 print(f"{name} ran {tests} tests")print(f"Pass rate: {passed / tests}")print(f"Upper: {name.upper()}, failed: {tests - passed}")
Output
Asha ran 40 tests
Pass rate: 0.925
Upper: ASHA, failed: 3

Format specifiers

After the expression, a colon starts the format specifier: {value:spec}.

Python
pi = 3.14159265print(f"{pi:.2f}")        # 2 decimal placesprint(f"{pi:.4f}")print(f"{1234567.891:,.2f}")   # thousands separatorprint(f"{0.8125:.1%}")    # percentageprint(f"{42:05d}")        # pad with zeros to width 5print(f"{255:x} {255:b}") # hex and binary
Output
3.14
3.1416
1,234,567.89
81.2%
00042
ff 11111111
SpecMeaningExampleResult
.2ffixed-point, 2 decimalsf"{3.14159:.2f}"3.14
,thousands separatorf"{1500000:,}"1,500,000
.1%percentage, 1 decimalf"{0.256:.1%}"25.6%
05dzero-pad to width 5f"{42:05d}"00042
>10right-align in 10 charsf"{'hi':>10}" hi
<10 / ^10left / centref"{'hi':^10}" hi
escientificf"{12345.678:.2e}"1.23e+04

Width and alignment

Fixed widths make columns line up. < aligns left, > right and ^ centre; the character before the arrow is the fill.

Python
rows = [("Keyboard", 2, 1499.5), ("Mouse", 10, 349), ("Monitor", 1, 12999)] print(f"{'Item':<10}{'Qty':>5}{'Price':>12}")print("-" * 27)for item, qty, price in rows:    print(f"{item:<10}{qty:>5}{price:>12,.2f}")print(f"{' Total ':=^27}")
Output
Item        Qty       Price
---------------------------
Keyboard      2    1,499.50
Mouse        10      349.00
Monitor       1   12,999.00
========== Total ==========

Debugging with =

Adding = after the expression prints the expression and its value:

Python
user_id = 7retries = 3print(f"{user_id=}, {retries * 2=}")
Output
user_id=7, retries * 2=6

Dates in f-strings

Date objects understand their own format codes (covered fully in the Datetime lesson):

Python
from datetime import date d = date(2026, 11, 2)print(f"{d:%d %B %Y}")print(f"{d:%Y-%m-%d} is a {d:%A}")
Output
02 November 2026
2026-11-02 is a Monday

Braces and quotes inside f-strings

Double the braces to print them literally. Use a different quote style for strings inside the expression:

Python
key = "id"print(f"{{{key}}}")data = {"id": 7}print(f"value: {data['id']}")
Output
{id}
value: 7

str.format()

str.format() uses the same {} placeholders and format specs. You will see it in older code and when a template is stored in a variable:

Python
template = "{name} scored {score:.1f}%"print(template.format(name="Ravi", score=87.456))print("{0} vs {1}, {0} wins".format("India", "Australia"))
Output
Ravi scored 87.5%
India vs Australia, India wins

The old % style

The oldest style uses % codes. It still appears in logging calls and old scripts:

Python
print("%s has %d items costing %.2f" % ("cart", 3, 499.999))
Output
cart has 3 items costing 500.00
Tip

Use f-strings for new code. They are the fastest and the easiest to read. Use str.format() only when the template must be defined before the values exist.

Check your understanding

Predict the output

x = 7.0 / 3print(f"[{x:.3f}] [{x:8.2f}] [{'ok':*^6}] [{1e6:,.0f}]")

Quick quiz

1. Which prints 0.50 for x = 0.5?

2. What does f"{0.25:.0%}" produce?

3. How do you print a literal { in an f-string?

Exercise: invoice line

Print each line as a 12-character left-aligned item name, the quantity right-aligned in 4 characters, and the line total with a thousands separator and 2 decimals in 12 characters.

Input
[("Laptop", 2, 54999), ("USB cable", 5, 199.5)]
Expected output
Laptop         2  109,998.00
USB cable      5      997.50
Hint 1

The line total is qty * price; use the spec >12,.2f.

Program Practice

Python string formatting FAQ

How do I format a float to 2 decimal places in Python?

Use an f-string: f"{value:.2f}". It returns a string; use round(value, 2) if you need a number.

What is an f-string in Python?

An f-string is a string literal prefixed with f in which {expression} placeholders are replaced by their values. They were added in Python 3.6.

How do I add commas to large numbers in Python?

Use the , format option: f"{1234567:,}" gives 1,234,567.

Which is better: f-string, format() or %?

f-strings are the most readable and the fastest. str.format() is useful for reusable templates, and % formatting is mainly found in older code and logging.

Leave a Comment