0
Formatting Numbers1 / 4

CRASH COURSE

Formatting Numbers

Inside an f-string, add a format spec after a colon: {price:.2f} shows exactly two decimals.

  • Use {n:05d} to pad with zeros, {n:,} for thousands separators, and {ratio:.0%} to show a percentage.
  • Formatting changes only the displayed text. The underlying number keeps its full precision.
price = 4.5
print(f"${price:.2f}")
print(f"{1234567:,}")