How to print a number using commas as thousands separators
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the simplest way to print a number with commas as thousands separators is to use format specifiers such as :,. This works in f-strings, format(...), and str.format(...), and it keeps the formatting rule explicit right where the value is displayed.
The Simplest Modern Form
For integers, an f-string is the cleanest option:
This prints:
The :, format specifier tells Python to insert commas every three digits.
Other Equivalent Forms
If you are not using f-strings, the same formatting rule works with format(...) or str.format(...).
All three approaches produce the same comma-separated output.
Formatting Floats
You can combine the thousands separator with decimal precision.
Output:
The comma controls grouping, and .2f or .3f controls decimal places.
Printing Without Changing the Underlying Number
Formatting creates a string representation. It does not change the numeric value itself.
This matters because once you format the number, you are dealing with text, not an integer or float.
Width and Alignment
You can combine grouping with alignment if you are printing tables or reports.
This is useful for console reports where columns need to line up cleanly.
Locale Is a Different Problem
If you specifically want commas, use the normal format specifier. If you want locale-aware grouping, that is a separate concern. Different locales may use periods, spaces, or other separators instead of commas.
For example, some regions would render one million differently. So the question "use commas as thousands separators" is not the same as "format according to local conventions."
If locale-based formatting is required, use locale-aware tools rather than hardcoding :, and assuming it is universally appropriate.
Negative Numbers and Zero
Grouping works naturally for negative numbers and zero as well.
Output:
No extra handling is needed for these normal cases.
Using the Formatted Value in Output Templates
In real programs, formatting usually happens at the final presentation step, such as a report line, log message, or CLI summary.
This keeps the numeric values numeric in the rest of your code and applies formatting only when rendering human-readable output.
Common Pitfalls
The biggest mistake is forgetting that formatting returns a string. If you need to keep doing arithmetic, continue using the original number and only format at display time.
Another issue is confusing a comma as a literal character in the string with the Python format specifier. The grouping only happens because the number is formatted, not because a comma was manually inserted.
Developers also sometimes use locale-aware formatting when the requirement is explicitly "always use commas." Those are different goals.
Finally, when formatting floats, remember that the number of decimal places still needs to be specified separately if you care about the exact printed precision.
Summary
- In Python, use
f"{number:,}"to print commas as thousands separators. - The same rule works with
format(number, ",")and"{:,}".format(number). - Combine grouping with precision for floats, such as
f"{value:,.2f}". - Formatting produces a string and does not change the numeric value.
- Locale-aware formatting is a separate problem from always using commas.

