How to Add a Thousands Separator to a Number in a Python f-string
228813984 is hard to read; 228,813,984 isn't. In an f-string, one comma after the colon groups a number's digits in threes. This guide prints a count, a file size and an amount with separators on Python 3.15.2.
Syntax
f"{<number>:<grouping>}"
f"{<number>:<grouping>.<decimals>f}"
| Part | What it means |
|---|---|
<number> | An integer or a float, or a variable that holds one |
<grouping> | , puts a comma every three digits, _ an underscore |
.<decimals>f | Optional. Shows a float with that many digits after the point |
The format specification documentation says the , option "Inserts a comma every 3 digits for integer presentation type 'd' and floating-point presentation types, excluding 'n'", and the _ option does the same with an underscore.
The Example
The number of photos in a dataset, the size of its archive in bytes, and an amount of money with two decimal places.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
photos = 3670
size = 228813984
print(f"{photos:,} photos")
print(f"{size:,} bytes")
print(f"{size:_} bytes")
print(f"{1234.5:,.2f}")

Step 2: Run It
uv run python thousands.py
3,670 photos
228,813,984 bytes
228_813_984 bytes
1,234.50

:, grouped the digits with commas and :_ with underscores. In :,.2f, the comma comes first and the decimals after it: the result has both a separator and two decimal places. The variables themselves are still plain numbers; only the printed text changed. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Formatting a number stored as text: f"{'3670':,}" | ValueError: Cannot specify ',' with 's'.: '3670' is a string, and strings take no separator | Convert it first: f"{int('3670'):,}" printed 3,670 |
Putting the comma after the type: :.2f, | ValueError: Invalid format specifier '.2f,' for object of type 'float' | Write :,.2f: the comma goes before the point |
| Expecting the country's own separator | , always uses a comma, whatever the computer's language settings | Use the locale-aware n type, which the documentation suggests "For a locale-aware separator" |
FAQ
Can I combine the separator with a width?
Yes, put the width first: f"[{3670:>8,}]" printed [ 3,670], the number right-aligned in 8 characters with its comma.
Why does Python also offer an underscore?
Underscores read well in code and logs, and Python accepts them in number literals too: 228_813_984 == 228813984 printed True.
Conclusion
f"{size:,}" printed 228,813,984, :_ used underscores, and :,.2f combined a separator with two decimal places. The value must be a number: convert text with int() or float() first.
Additional Resources
- Format Specification Mini-Language — every formatting option, including grouping.
- PEP 378 — the proposal that added the comma separator.
