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.

Answer from Ian Schneider on Stack Overflow
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
408

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.

Discussions

Print Number with Commas in Python - Ask a Question - TestMu AI Community
How can I print a number with commas as thousands separators in Python? For example, I want to convert the integer 1234567 into 1,234,567. It does not need to be locale-specific, meaning I just want to use commas as the separator, not considering periods or other locale-specific formatting rules. More on community.testmuai.com
🌐 community.testmuai.com
0
December 25, 2024
Format Integer With Comma Using Python Printf
I have built a slider that has integer values between, say, 0 and 2,000,000. I would like to display the numbers with thousands-commas-separator. The documentation states: format (str or None) – A printf-style format string controlling how the interface should display numbers. More on discuss.streamlit.io
🌐 discuss.streamlit.io
0
0
March 30, 2020
Can anyone help me with this problem?
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_number(1000000) should return "1,000,000". More on discuss.python.org
🌐 discuss.python.org
0
0
November 19, 2022
python - Is there a way to format a number with commas for thousands, without converting the int to a string? - Stack Overflow
I'd like to add commas to 'separate the thousands' when long integers are printed. The catch is, I don't want to convert the int to a string. There are a few questions in SO that suggest something ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-35.php
Python: Display a number with a comma separator - w3resource
June 12, 2025 - 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 ...
🌐
Python Guides
pythonguides.com › python-format-number-with-commas
How To Format Numbers With Commas In Python?
December 22, 2025 - F-strings, introduced in Python 3.6, provide one of the best ways to format strings, including numbers with commas. Here’s how you can use f-strings to format numbers in Python. Here is an example. number = 1234567890 formatted_number = f"{number:,}" print(formatted_number)
🌐
Alexwlchan
alexwlchan.net › notes › 2025 › python-comma-n
Print a comma-separated number in Python with {num:,} – alexwlchan
May 28, 2025 - 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 › print-number-commas-1000-separators-python
Print number with commas as 1000 separators in Python - GeeksforGeeks
May 14, 2025 - Format the number and add commas as a thousand separators to use the ',d' formatting syntax in the format() function. ... F-string with replaces function. ... Here, we have used the "{:,}" along with the format() function to add commas every ...
🌐
AskPython
askpython.com › home › adding commas into number string
Adding commas into number string - AskPython
February 27, 2023 - If we want to round the number to 3 places, we can do it by replacing 2 with 3. num = 2232890.82728 print("The Number: ", num) print("Result : {:,.2f}".format(num)) ... import re num = 2232890 print("The original number is : " + str(num)) res = re.sub(r'(\d{2})(?=\d)', r'\1,', str(num)[::-1])[::-1] print("The number after inserting commas : " + str(res)) ... This line of code utilizes the re.sub() function from the Python re module to execute a search and replace of a regular expression on a string named num.
Find elsewhere
🌐
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 - This tutorial will demonstrate different ways of formatting a number with commas in Python. The format() is a built-in function that generally helps in string handling. This function also helps in changing complex variables and handles value formatting. ... initial_num = 1000000000 thousand_sep = format(initial_num, ",d") print("Number before inserting commas : " + str(initial_num)) print("Number after inserting commas: " + str(thousand_sep))
🌐
Finxter
blog.finxter.com › how-to-print-an-integer-with-commas-as-thousands-separators-in-python
How to Print an Integer with Commas as Thousands Separators in Python? – Be on the Right Side of Change
If you use points as a thousand-separator—for example in 1.000.000 as done in Europe—you can replace the commas in the comma-separated number using the suffix .replace(',', '.') in '{:,}'.format(x).replace(',','.') for any integer number x. >>> '{:,}'.format(1000000).replace(',','.') '1.000.000' ... An alternative way to add commas as thousand separators is to use the ',d' formatting syntax in the format() function. ... Source: https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators
🌐
TestMu AI Community
community.testmuai.com › ask a question
Print Number with Commas in Python - Ask a Question - TestMu AI Community
December 25, 2024 - How can I print a number with commas as thousands separators in Python? For example, I want to convert the integer 1234567 into 1,234,567. It does not need to be locale-specific, meaning I just want to use commas as the…
🌐
Streamlit
discuss.streamlit.io › using streamlit
Format Integer With Comma Using Python Printf - Using Streamlit - Streamlit
March 30, 2020 - I have built a slider that has integer values between, say, 0 and 2,000,000. I would like to display the numbers with thousands-commas-separator. The documentation states: format (str or None) – A printf-style format st…
🌐
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.
🌐
AskPython
askpython.com › home › how to print a number using commas as separators?
How to print a number using commas as separators? - AskPython
March 31, 2023 - The curly braces contain data that we want to replace the string. Let’s see the example of using an f-string as a commas separator. ... Here, the ‘Number’ is assigned with the integer value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-add-comma-between-numbers
Add Comma Between Numbers - Python - GeeksforGeeks
July 12, 2025 - format() function in Python provides a simple and efficient way to add commas between numbers. ... The :, within the curly braces tells Python to insert commas as a thousand separator.
🌐
Littlecolumns
littlecolumns.com › learn › python › f-strings
Formatting output with f strings - Python's Not (Just) For Unicorns
When you’re using your variable, you can also add weird little modifiers to tell Python how you want the variable displayed. It’s mostly used with numbres - you can add commas to big numbers, round long decimal numbers, left- or right-align, or a hundred other things! years = 65000000 print(f'Tyrannosaurus rex roamed the earth {years:,} years ago') pi_value = 3.14159265 print(f'The value of pi is roughly {pi_value:.3f}') id_number = 3 print(f'Your ID number is {id_number:04}')
Top answer
1 of 4
104

No one so far has mentioned the new ',' option which was added in version 2.7 to the Format Specification Mini-Language -- see PEP 378: Format Specifier for Thousands Separator in the What's New in Python 2.7 document. It's easy to use because you don't have to mess around with locale (but is limited for internationalization due to that, see the original PEP 378). It works with floats, ints, and decimals — and all the other formatting features provided for in the mini-language spec.

Sample usage:

print format(1234, ",d")    # -> 1,234
print "{:,d}".format(1234)  # -> 1,234
print(f'{1234:,d}')         # -> 1,234 (Python 3.6+)

Note: While this new feature is definitely handy, it's actually not all that much harder to use the locale module, as several others have suggested. The advantage is that then numeric output can be made to automatically follow the proper thousands (and other) separator conventions used in various countries when outputting things like numbers, dates, and times. It's also very easy to put the default settings from your computer into effect without learning a bunch of language and country codes. All you need to do is:

import locale
locale.setlocale(locale.LC_ALL, '')  # empty string for platform's default settings

After doing that you can just use the generic 'n' type code for outputting numbers (both integer and float). Where I am, commas are used as the thousand separator, so after setting the locale as shown above, this is what would happen:

print format(1234, "n")    # -> 1,234
print "{:n}".format(1234)  # -> 1,234

Much of the rest of the world uses periods instead of commas for this purpose, so setting the default locale in many locations (or explicitly specifying the code for such a region in a setlocale() call) produces the following:

print format(1234, "n")    # -> 1.234
print "{:n}".format(1234)  # -> 1.234

Output based on the 'd' or ',d' formatting type specifier is unaffected by the use (or non-use) of setlocale(). However the 'd' specifier is affected if you instead use the locale.format() or locale.format_string() functions.

2 of 4
13

locale.format()

Don't forget to set the locale appropriately first.

🌐
Codemia
codemia.io › knowledge-hub › path › how_to_print_a_number_using_commas_as_thousands_separators
How to print a number using commas as thousands ...
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
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 - To format thousand separators for ... f"{x:,}" In this function, we use Python’s f-string formatting syntax to format the number with commas as thousand separators ({x:,})....
🌐
Bobby Hadz
bobbyhadz.com › blog › python-format-number-thousands-separator-2-decimals
Format number with comma as thousands separator in Python | bobbyhadz
April 9, 2024 - Copied!import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_float = 15467.3 result = locale.currency(my_float, grouping=True, symbol=True) print(result) # 👉️ $15,467.30 result = locale.currency(my_float, grouping=False, symbol=False) print(result) # 👉️ 15467.30 · You can set the grouping keyword argument to False to remove the comma thousands separator. You can learn more about the related topics by checking out the following tutorials: Using f-string for conditional formatting in Python