number formatting
thousands separator
Python programming
coding tutorial
data presentation

How to print a number using commas as thousands separators

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

python
number = 1000000
print(f"{number:,}")

This prints:

text
1,000,000

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(...).

python
1number = 1000000
2
3print(format(number, ","))
4print("{:,}".format(number))

All three approaches produce the same comma-separated output.

Formatting Floats

You can combine the thousands separator with decimal precision.

python
1value = 1234567.8912
2
3print(f"{value:,.2f}")
4print(format(value, ",.3f"))

Output:

text
1,234,567.89
1,234,567.891

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.

python
1number = 1000000
2formatted = f"{number:,}"
3
4print(formatted)
5print(type(formatted))
6print(number + 1)

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.

python
1number = 1234567
2
3print(f"|{number:>15,}|")
4print(f"|{number:<15,}|")

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.

python
print(f"{0:,}")
print(f"{-1234567:,}")

Output:

text
0
-1,234,567

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.

python
1count = 1234567
2total = 9876543.21
3
4print(f"Processed {count:,} rows")
5print(f"Total revenue: ${total:,.2f}")

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.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.