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.

🌐
Python
peps.python.org › pep-0378
PEP 378 – Format Specifier for Thousands Separator | peps.python.org
March 12, 2009 - format(1234, "8.1f") --> ' 1234.0' format(1234, "8,1f") --> ' 1234,0' format(1234, "8.,1f") --> ' 1.234,0' format(1234, "8 ,f") --> ' 1 234,0' format(1234, "8d") --> ' 1234' format(1234, "8,d") --> ' 1,234' format(1234, "8_d") --> ' 1_234' This proposal meets mosts needs, but it comes at the expense of taking a bit more effort to parse. Not every possible convention is covered, but at least one of the options (spaces or underscores) should be readable, understandable, and useful to folks from many diverse backgrounds. As shown in the examples, the width argument means the total length including the thousands separators and decimal separators.
Discussions

Add underscore as a thousandths separator for string formatting - Ideas - Discussions on Python.org
I can think of two different ways to do this: Special case the _ grouping_option to add an underscore in the thousandths places Add a new format specifier (like float_grouping): format_spec ::= [[fill]align][sign… More on discuss.python.org
🌐 discuss.python.org
1
February 26, 2021
Thousands Separator in a Number Column of Data Editor
Summary How to show thousands separator in a Number Column inside the Data Editor component introduced in streamlit 1.23? I tried to set the number format to “%,.2f” as it is recognized by the defautl pandas DataFrame Styler, but this format results in erro when used in the Data Editor. More on discuss.streamlit.io
🌐 discuss.streamlit.io
0
0
June 14, 2023
python format string thousand separator with spaces - Stack Overflow
For printing number with thousand separator, one can use the python format string : '{:,}'.format(1234567890) But how can I specify that I want a space for thousands separator? More on stackoverflow.com
🌐 stackoverflow.com
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.org
discuss.python.org › ideas
Space as a thousands separator in the format specification mini-language - Ideas - Discussions on Python.org
December 1, 2024 - Could I put in a word for spaces as a grouping option in the format specification mini-language? Right now, comma and underscore are available – why not space as well? One doesn’t always want to change the locale to get the international standard for representation of numbers.
🌐
Python.org
discuss.python.org › ideas
Add underscore as a thousandths separator for string formatting - Ideas - Discussions on Python.org
February 26, 2021 - I can think of two different ways to do this: Special case the _ grouping_option to add an underscore in the thousandths places Add a new format specifier (like float_grouping): format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.pr...
🌐
Streamlit
discuss.streamlit.io › using streamlit
Thousands Separator in a Number Column of Data Editor - Using Streamlit - Streamlit
June 14, 2023 - Summary How to show thousands separator in a Number Column inside the Data Editor component introduced in streamlit 1.23? I tried to set the number format to “%,.2f” as it is recognized by the defautl pandas DataFrame S…
🌐
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.
🌐
Sololearn
sololearn.com › en › Discuss › 3235225 › thousand-separator-for-float-nums
thousand separator for float nums? | Sololearn: Learn to code for FREE!
August 25, 2023 - #numbers with thousands separator print(format(10000,",d")) print(format(10000.50,",d")) for the second line i get a value error for float nums is there another way to get the output as 10,000.50 https://code.sololearn.com/clRdE90GjCEV/?ref=app
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 - We then apply the format_int_with_commas function to each element of the DataFrame using the map() method. The resulting DataFrame contains the same values, but with thousand separators added for readability. Note that the map() method applies the function to each element of the DataFrame, so it works for both integer and float data types.
🌐
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
February 5, 2021 - Now you can switch locales and both parse and format numbers and dates in the proper way for that locale. You can also switch between locales when needed. In [1]: import locale ...: locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') Out[1]: 'de_DE.UTF-8' In [2]: locale.currency(0.5) Out[2]: '0,50 €' In [3]: locale.currency(1000.5, grouping=True) # thousands separator Out[3]: '1.000,50 €' In [4]: print('eine halbe Einheit: ' + locale.format_string('%.2f', 0.5)) eine halbe Einheit: 0,50 In [5]: locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') Out[5]: 'en_US.UTF-8' In [6]: print('half a unit: ' + locale.format_string('%.2f', 0.5)) half a unit: 0.50
🌐
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 - On each iteration, we use a formatted string literal to format the current float with a comma as the thousands separator to 2 decimal places and return the result.
Top answer
1 of 10
140

Here is bad but simple solution if you don't want to mess with locale:

'{:,}'.format(1234567890.001).replace(',', ' ')
2 of 10
38

Answer of @user136036 is quite good, but unfortunately it does not take into account reality of Python bugs. Full answer could be following:

Variant A

If locale of your platform is working right, then just use locale:

import locale
locale.setlocale(locale.LC_ALL, '')
print("{:,d}".format(7123001))

Result is dependent on your locale and Python implementation working right.

But what if Python formatting according to locale is broken, e.g. Python 3.5 on Linux?

Variant B

If Python does not respect grouping=True parameter, you can use locale and a workaround (use monetary format):

locale.setlocale(locale.LC_ALL, '')
locale._override_localeconv = {'mon_thousands_sep': '.'}
print(locale.format('%.2f', 12345.678, grouping=True, monetary=True))

Above gives 12.345,68 on my platform. Setting monetary to False or omitting it - Python does not group thousands. Specifying locale._override_localeconv = {'thousands_sep': '.'} do nothing.

Variant C

If you don't have time to check what is working OK and what is broken with Python on your platform, you can just use regular string replace function (if you want to swap commas and dot to dots and comma):

print("{:,.2f}".format(7123001.345).replace(",", "X").replace(".", ",").replace("X", "."))

Replacing comma for space is trivial (point is assumed decimal separator):

print("{:,.2f}".format(7123001.345).replace(",", " ")

🌐
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.
🌐
Towards Data Science
towardsdatascience.com › home › latest › apply thousand separator (and other formatting) to pandas dataframe
Apply Thousand Separator (and Other Formatting) to Pandas Dataframe | Towards Data Science
January 28, 2025 - We use the python string format syntax '{:,.0f}'.format to add the thousand comma separators to the numbers. Then we use python’s map() function to iterate and apply the formatting to all the rows in the ‘Median Sales Price’ column. ... Changing the syntax to '{:,.2f}'.format will give you numbers with two decimal places.
🌐
Python Guides
pythonguides.com › python-format-number-with-commas
How To Format Numbers With Commas In Python?
December 22, 2025 - 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, like this: formatted_number = f"{number:,}". This will format the number with commas as thousand separators, ...
🌐
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
Using the modern f-strings is, in my opinion, the most Pythonic solution to add commas as thousand-separators for all Python versions above 3.6: f'{1000000:,}'. The inner part within the curly brackets :, says to format the number and use commas as thousand separators.
🌐
Reddit
reddit.com › r/python › thousands separator using underscore
r/Python on Reddit: Thousands separator using underscore
June 1, 2019 - It works in Python in most cases, but depending on formatting and new lines you may need to cover it in a parenthesis. Works in C/C++ too (but be careful with macros and this concatenation, it's a little tricky. Also in Python: print takes input as variadic arguments, with a keyword argument for the separator. Default is a space. ... I think Dutch or French writes thousands seperators like this. Its not like they decided for ' as seperator out of the blue.
🌐
ActiveState
code.activestate.com › recipes › 498181-add-thousands-separator-commas-to-formatted-number
Add thousands separator commas to formatted numbers « Python recipes « ActiveState Code
October 7, 2006 - to handle more types that happen to look numeric when stringified, and also to work better on Python 3.x, which doesn't have long. ... This version factors out the recursion to reduce the number of redundant checks. splitThousandsPosInt only handles strings of digits (or whatever Garbage In). splitThousands handles any number of leading spaces or signs, and stops at a decimal point. GIGO, of course. Pass in properly formatted numbers and it will work.
🌐
Python.org
discuss.python.org › python help
Can Python have support for number separators? - Python Help - Discussions on Python.org
February 23, 2024 - I see that when I call something like int(2,345,565), it gives an error. Interestingly, in Java, this is supported. Is it a good idea to have something like this in Python?
🌐
Phrase
phrase.com › home › resources › blog › how do i convert a decimal to a string with thousands separators?
How Do I Convert a Decimal to a String with Thousands Separators?
January 23, 2025 - When converting codebase numbers into locale-aware strings, we often need to take care of thousands separators. Here’s how to get it done easily. This is a common concern when working with i18n. We have a number as a double or float, and we want to present it to our users as a string with proper, locale-aware formatting.