Advanced f-strings — Formatting Dates and Numbers

Published:
Last updated:
By Jeferson Peter
Python

f-strings are one of Python’s most popular features for string formatting.
Beyond simple interpolation, they let you format numbers, dates, and even apply expressions directly inside the braces.


Formatting numbers

value = 1234.56789
print(f"{value:.2f}")   # 2 decimal places
print(f"{value:,}")     # thousands separator

# 1234.57
# 1,234.568

Formatting percentages

progress = 0.756
print(f"{progress:.1%}")

# 75.6%

Formatting dates

from datetime import datetime

today = datetime(2025, 9, 6)
print(f"{today:%Y-%m-%d}")   # ISO format
print(f"{today:%B %d, %Y}")  # Month name

# 2025-09-06
# September 06, 2025

Expressions inside f-strings

name = "alice"
print(f"Hello, {name.upper()}!")

# Hello, ALICE!

Conclusion

f-strings make formatting cleaner and more readable.
They’re not just for variables, but also for numbers, dates, and inline expressions.