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

'{:,}'.format(1234567890.001).replace(',', ' ')
Answer from Raz on Stack Overflow
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 › 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.
🌐
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
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
🌐
Python
peps.python.org › pep-0378
PEP 378 – Format Specifier for Thousands Separator | peps.python.org
The user can supply an alternate set of symbols using the formatter’s DecimalFormatSymbols object. Make both the thousands separator and decimal separator user specifiable but not locale aware. For simplicity, limit the choices to a COMMA, DOT, SPACE, APOSTROPHE or UNDERSCORE.
🌐
Sololearn
sololearn.com › en › Discuss › 3235225 › thousand-separator-for-float-nums
thousand separator for float nums? | Sololearn: Learn to code for FREE!
Angela , This is a sample that ... specs will follow , (comma): Grouping separator for thousands. . (dot): Separator for floating point numbers....
🌐
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 ...
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.

🌐
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 integers in a pandas DataFrame, we can define a function that takes a number as input and returns a string representation of the number with thousand separators. def format_int_with_commas(x): """ Formats an ...
Find elsewhere
🌐
Peterbe.com
peterbe.com › plog › format-thousands-in-python
Format thousands in Python - Peterbe.com
>>> number = 1234567890 >>> f"{number:,}" '1,234,567,890' ... All of this and more detail can be found in PEP 378 -- Format Specifier for Thousands Separator.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-format-numbers-with-thousand-separators-in-python
5 Best Ways to Format Numbers with Thousand Separators in Python – Be on the Right Side of Change
This method uses Python’s built-in format() function to insert commas as thousand separators. The function works by specifying a format string, which in this case is {:,}, where the colon acts as a separator specifier and the comma indicates the type of separator.
🌐
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.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-format-number-thousands-separator-2-decimals
Format number with comma as thousands separator in Python | bobbyhadz
As noted in the docs, the , option signals the use of a comma for a thousands separator. The n integer presentation type is used for a locale-aware separator. You can also use the str.format() method to format a number with a comma as the thousands separator.
🌐
TutorialsPoint
tutorialspoint.com › program-to-find-number-with-thousand-separator-in-python
Program to find number with thousand separator in Python
Suppose we have a number n, we have to return this number into string format where thousands are separated by comma (","). So, if the input is like n = 512462687, then the output will be "512,462,687" To solve this,
🌐
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 - 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. df.loc[:, "Median Sales Price_formatted"] ='$'+ df["Median Sales Price"].map('{:,.2f}'.format) ... If the "Median Sales Price’ column is an integer type, then you can also use the following code to add the thousand comma separators:
🌐
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][.precision[float_grouping]][type] fill ::= align ::= " " | "=" | "^" sign ::= "+" | "-" | " " width ::= digit+ grouping_option ::= "_" | "," float_grouping ::= "_" precision ...
🌐
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 - import java.util.Locale; import java.text.NumberFormat; public class FormatNumbers { public static void main(String []args) { double number = 123456.78; NumberFormat usFormatter = NumberFormat.getInstance(new Locale("en", "US")); System.out.println(usFormatter.format(number)); // => 123,456.78 (American format) NumberFormat frenchFormatter = NumberFormat.getInstance(new Locale("fr", "FR")); System.out.println(frenchFormatter.format(number)); // => 123 456,78 (French format) NumberFormat thaiFormatter = NumberFormat.getInstance(new Locale("th", "TH", "TH")); System.out.println(thaiFormatter.format(number)); // => ๑๒๓,๔๕๖.๗๘ (Thai format) } } Python’s built-in localized number formatting is such a pain to use, I strongly recommend that you employ a third-party library for the job.
🌐
TutorialsPoint
tutorialspoint.com › print-number-with-commas-as-1000-separators-in-python
Print number with commas as 1000 separators in Python
This is a requirement mainly in the accounting industry as well as in the finance domain. In this article we'll see how Python program can be used to insert a comma at a suitable place. We are aiming to insert comma as a thousand separator. The format function in python can be used with below settings to achieve this requirement.
🌐
ActiveState
code.activestate.com › recipes › 498181-add-thousands-separator-commas-to-formatted-number
Add thousands separator commas to formatted numbers « Python recipes « ActiveState Code
##################################### def _splitThousandsHelper( s, tSep ): if len( s ) <= 3: return s return _splitThousandsHelper( s[ :-3 ], tSep ) + tSep + s[ -3: ] ##################################### def splitThousandsPosInt( s, tSep=',' ): if not isinstance( s, str ): s = str( s ) return _splitThousandsHelper( s, tSep ) ##################################### # http://code.activestate.com/recipes/498181-add-thousands-separator-commas-to-formatted-number/ # Code from Michael Robellard's comment made 28 Feb 2010 # Modified for leading +, -, space on 1 Mar 2010 by Glenn Linderman def splitTh