To format a number with commas in Python, put a comma in the format spec: f"{n:,}" turns 1234567 into 1,234,567. Add a precision for floats, like f"{n:,.2f}":
n = 1234567.5
print(f"{n:,}") # 1,234,567.5
print(f"{n:,.2f}") # 1,234,567.50
Everything below ran on Python 3.12.5 and pandas 3.0.6, with US numbers like state populations and dollar revenue.
We’ll cover f-strings, format(), other separators, locale-aware output, pandas columns and turning formatted text back into numbers.
Format a number with commas using an f-string
The , option groups digits in threes. It works on ints and floats, keeps the minus sign, and leaves numbers under 1,000 alone:
population = 39431263 # California, 2024 estimate
revenue = 1284567.5
print(f"{population:,}") # int with commas
print(f"{revenue:,.2f}") # float: commas + 2 decimals
print(f"${revenue:,.2f}") # USD amount
print(f"{-7500000:,}") # negatives work too
print(f"{1234:,}", f"{999:,}") # short numbers stay short
Output:
39,431,263
1,284,567.50
$1,284,567.50
-7,500,000
1,234 999

The .2f part is ordinary decimal formatting; formatting decimal places with f-strings explains it in depth.
Add commas with format() and str.format()
The same spec works outside f-strings. That’s handy when the pattern is stored in a variable or you’re on older code. Python also accepts _ as the separator:
sales = 2750000.456
print(format(sales, ",.2f")) # built-in format()
print("{:,}".format(48213)) # str.format()
print("Total: {:>15,.2f}".format(sales)) # width + commas
print(format(1048576, "_")) # underscore separator
print(format(255000, "_x")) # "_" groups hex in 4s
Output:
2,750,000.46
48,213
Total: 2,750,000.46
1_048_576
3_e418

format() and str.format() give the same commas; _ groups decimals in threes and hex digits in fours.If you need literal braces next to the placeholder, see escaping curly braces in Python format strings.
How do I use a period or space as the thousands separator?
The format spec only knows , and _. For European style, format the US way first, then swap the symbols in one pass with str.translate():
amount = 1234567.89
us = f"{amount:,.2f}"
eu = us.translate(str.maketrans(",.", ".,")) # swap the two symbols
space = us.replace(",", " ")
print("US: ", us)
print("EU: ", eu)
print("Space:", space)
Output:
US: 1,234,567.89
EU: 1.234.567,89
Space: 1 234 567.89
Don’t chain two replace() calls for the swap, because the second one undoes the first. Building output strings in general is covered in creating a string with variables in Python.
Format numbers with commas for a locale
When users in different countries see your output, let the locale module pick the symbols. The n format type and locale.format_string(..., grouping=True) both follow the active locale:
import locale
n = 1234567.891
for name in ("en_US.UTF-8", "de_DE.UTF-8"):
locale.setlocale(locale.LC_ALL, name)
print(f"{name:12} {locale.format_string('%.2f', n, grouping=True):>14} {round(n):n}")
locale.setlocale(locale.LC_ALL, "") # back to the system default
Output:
en_US.UTF-8 1,234,567.89 1,234,568
de_DE.UTF-8 1.234.567,89 1.234.568

Locale settings are process-wide, so I set them once at startup rather than inside a loop in real apps. The locale names must exist on the machine.
Add commas to a pandas column
For a DataFrame, map a format string over the column. Keep the original numbers and store the formatted text in a new column, since text can’t be summed:
import pandas as pd
df = pd.DataFrame({"state": ["California", "Texas", "Florida"],
"revenue": [1284567.5, 948210.25, 731004.0]})
df["revenue_fmt"] = df["revenue"].map("{:,.2f}".format) # new text column
print(df)
print(df["revenue"].dtype, "|", df["revenue_fmt"].dtype)
Output:
state revenue revenue_fmt
0 California 1284567.50 1,284,567.50
1 Texas 948210.25 948,210.25
2 Florida 731004.00 731,004.00
float64 | str

revenue_fmt column shows commas, while revenue stays a float64 you can still do math on.To show money with a dollar sign instead, follow formatting numbers as currency in Python.
Convert a string with commas back to a number
Going the other way, strip the commas before calling float() or int(); both raise ValueError on "1,284":
text = "1,284,567.50"
value = float(text.replace(",", ""))
print(value, type(value).__name__)
print(int("39,431,263".replace(",", "")) + 1)
Output:
1284567.5 float
39431264
For more on that conversion, read converting a string to an integer in Python.
Comma formatting options compared
| Code | Result for 1234567.891 |
|---|---|
f"{n:,}" | 1,234,567.891 |
f"{n:,.2f}" | 1,234,567.89 |
f"{n:_.2f}" | 1_234_567.89 |
f"{round(n):n}" (de_DE locale) | 1.234.568 |
You might also like these related guides:
- Print 2 decimal places in Python
- Round numbers to 2 decimal places in Python
- Print an array with commas in Python
- Pad a string with zeros in Python
Frequently asked questions
How do I format a number with commas in Python?
Use f"{n:,}", for example f"{1234567:,}" gives '1,234,567'.
How do I add commas and 2 decimal places?
Combine both options: f"{n:,.2f}" gives '1,284,567.50' for 1284567.5.
How do I print a dollar amount with commas?
Put the sign outside the braces: f"${n:,.2f}".
Can I use spaces or periods instead of commas?
Not in the spec itself. Format with commas, then use replace() or translate(), or use the locale module.
How do I remove commas from a number string?
Use float(s.replace(",", "")) or int(s.replace(",", "")).
Where is the comma option documented?
See the Format Specification Mini-Language in the Python docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn