In Python 2.7 and 3.x you can use str.format for this:

>>> num = 1234567890.0876543
>>> "{0:,f}".format(num)
'1,234,567,890.087654'
>>> "{0:,.2f}".format(num)
'1,234,567,890.08'
>>> "{0:,f}".format(1234)
'1,234.000000'
Answer from Ashwini Chaudhary on Stack Overflow
🌐
Python Guides
pythonguides.com › python-format-number-with-commas
How To Format Numbers With Commas In Python?
January 16, 2025 - In this tutorial, I will explain in detail how to format numbers with commas in Python using different methods. To format numbers with commas in Python, you can use f-strings, which were introduced in Python 3.6. Simply embed the number within curly braces and use a colon followed by a comma, ...
Top answer
1 of 16
2569

Locale-agnostic: use _ as the thousand separator

f'{value:_}'          # For Python ≥3.6

Note that this will NOT format in the user's current locale and will always use _ as the thousand separator, so for example:

1234567 ⟶ 1_234_567

English style: use , as the thousand separator

'{:,}'.format(value)  # For Python ≥2.7
f'{value:,}'          # For Python ≥3.6

Locale-aware

import locale
locale.setlocale(locale.LC_ALL, '')  # Use '' for auto, or force e.g. to 'en_US.UTF-8'

'{:n}'.format(value)  # For Python ≥2.7
f'{value:n}'          # For Python ≥3.6

Reference

Per Format Specification Mini-Language,

The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.

and:

The '_' option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type 'd'. For integer presentation types 'b', 'o', 'x', and 'X', underscores will be inserted every 4 digits.

2 of 16
407

I'm surprised that no one has mentioned that you can do this with f-strings in Python 3.6+ as easy as this:

>>> num = 10000000
>>> print(f"{num:,}")
10,000,000

... where the part after the colon is the format specifier. The comma is the separator character you want, so f"{num:_}" uses underscores instead of a comma. Only "," and "_" is possible to use with this method.

This is equivalent of using format(num, ",") for older versions of python 3.

This might look like magic when you see it the first time, but it's not. It's just part of the language, and something that's commonly needed enough to have a shortcut available. To read more about it, have a look at the group subcomponent.

🌐
Python.org
discuss.python.org › python help
Can anyone help me with this problem? - Python Help - Discussions on Python.org
November 19, 2022 - Write a function named format_number that takes a non-negative number as its only parameter. Your function should convert the number to a string and add commas as a thousands separator. For example, calling format_numb…
🌐
AskPython
askpython.com › home › adding commas into number string
Adding commas into number string - AskPython
February 27, 2023 - To illustrate, using the thousands separator specifier, an integer can be formatted with commas. In addition to the format() function, the locale module is another useful tool for formatting integers based on a user’s system preferences, including the symbols for thousands separators and decimal points.
🌐
Delft Stack
delftstack.com › home › howto › python › python format number with commas
How to Format Number With Commas in Python | Delft Stack
February 2, 2024 - String formatters are represented by curly braces {} that work by mentioning the replacement parameters and the place of those parameters. ... In this method, we first define a function called thousand_sep with its argument as the number in which commas are inserted. After that, we call the str.format() with the string as the string formatter. In the string formatter, we mention the replacement parameter i.e ,. Finally, we print the defined function. F-strings is again a string formatting technique in Python.
🌐
GeeksforGeeks
geeksforgeeks.org › python › print-number-commas-1000-separators-python
Print number with commas as 1000 separators in Python - GeeksforGeeks
May 14, 2025 - F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting. The inner part within the curly brackets : , says to format the number and use commas as thousand separators.
Find elsewhere
🌐
Saturn Cloud
saturncloud.io › blog › how-to-format-thousand-separator-for-integers-in-a-pandas-dataframe
How to Format Thousand Separator for Integers in a Pandas DataFrame | Saturn Cloud Blog
December 6, 2023 - The comma in the curly braces tells Python to use the comma as a thousand separator. We can then apply this function to each element of the DataFrame using the map() method. import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'A': [1000, 2000000, 300000000], 'B': [4000, 5000000, 600000000], 'C': [7000, 8000000, 900000000] }) # Apply the format_int_with_commas function to each element of the DataFrame df = df.map(format_int_with_commas) print(df)
🌐
mkaz.blog
mkaz.blog › working-with-python › string-formatting
Python String Formatting: Complete Guide
value = 1234.5678 # Basic decimal places print(f"Two decimals: {value:.2f}") # 1234.57 print(f"No decimals: {value:.0f}") # 1235 print(f"With sign: {value:+.2f}") # +1234.57 # Padding and alignment print(f"Right aligned: {value:10.2f}") # 1234.57 print(f"Left aligned: {value:<10.2f}") # 1234.57 print(f"Center aligned: {value:^10.2f}") # 1234.57 print(f"Zero padded: {value:010.2f}") # 001234.57 # Thousands separator print(f"With commas: {value:,.2f}") # 1,234.57 # Percentage ratio = 0.857 print(f"Percentage: {ratio:.1%}") # 85.7% # Scientific notation big_number = 1500000 print(f"Scientific: {big_number:.2e}") # 1.50e+06 # Different bases num = 255 print(f"Hex: {num:x}") # ff print(f"Binary: {num:b}") # 11111111 print(f"Octal: {num:o}") # 377
🌐
Herrmann
herrmann.tech › en › blog › 2021 › 02 › 05 › how-to-deal-with-international-data-formats-in-python.html
How to deal with international data formats in Python – herrmann.tech
The thousands separator is the other mark. That is, in countries that use the dot as decimal, the comma is the thousands separator and vice versa. ... What people often do when interpreting those numbers with Python is simply using the replace method of the str class.
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-35.php
Python: Display a number with a comma separator - w3resource
print("Formatted Number with comma separator: "+"{:,}".format(x)) # Print the original value of 'y' with a label. print("Original Number: ", y) # Format the value of 'y' with a comma separator for thousands and print it. print("Formatted Number ...
🌐
Invent with Python
inventwithpython.com › pythongently › exercise33
Exercise 33 - Comma-Formatted Numbers
There is a comma after every third digit in the whole number part. There are no commas at all in the fractional part: The proper comma formatting of 1234.5678 is 1,234.5678 and not 1,234.567,8. These Python assert statements stop the program if their condition is False.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-format-number-thousands-separator-2-decimals
Format number with comma as thousands separator in Python | bobbyhadz
You can also use a formatted-string literal to format a float as currency. You can use an expression in the f-string to format the number with a comma as the thousands separator, rounded to 2 decimal places.
🌐
Linux find Examples
queirozf.com › entries › python-number-formatting-examples
Python number formatting examples
August 2, 2023 - # ValueError in python 2.6 and 3.0 a=1 b=2 "{}-{}".format(a,b) # NO ERROR in any python version "{0}-{1}".format(a,b) # >>> "1-2"
🌐
Alexwlchan
alexwlchan.net › notes › 2025 › python-comma-n
Print a comma-separated number in Python with {num:,} – alexwlchan
You can use `{num:,}` to insert a comma every three digits, `{num:_}` to insert an underscore every three digits, and `{num:n}` to insert a locale-aware digit separator.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-add-comma-between-numbers
Add Comma Between Numbers - Python - GeeksforGeeks
July 12, 2025 - This method involves manually looping through the digits of the number and inserting commas after every third digit. While functional, it requires more code and is less efficient compared to using built-in methods like format() or f-strings. re.sub() function from Python’s regular expressions module can also be used to insert commas into a number string.
🌐
Littlecolumns
littlecolumns.com › learn › python › f-strings
Formatting output with f strings - Python's Not (Just) For Unicorns
Hint: 7200000 needs some commas! Check the example above to see how we did it when talking about dinosaurs. Hint: If you’re doing math in the curly brace, the :, goes before the }, not after the variable name. ... Hint: 7,200,000.0 looks kind of weird, we don’t need that decimal.