Use locale.format():

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'German')
'German_Germany.1252'
>>> print(locale.format('%.2f', 32757121.33, True))
32.757.121,33

You can restrict the locale changes to the display of numeric values (when using locale.format(), locale.str() etc.) and leave other locale settings unaffected:

>>> locale.setlocale(locale.LC_NUMERIC, 'English')
'English_United States.1252'
>>> print(locale.format('%.2f', 32757121.33, True))
32,757,121.33
>>> locale.setlocale(locale.LC_NUMERIC, 'German')
'German_Germany.1252'
>>> print(locale.format('%.2f', 32757121.33, True))
32.757.121,33
Answer from Tim Pietzcker 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

f-string - converting float to string with set decimals and thousand separator
import locale a = 1100300.506 locale.setlocale(locale.LC_ALL, 'no_NO') locale._override_localeconv = {'thousands_sep': ' ','decimal_point': '.'} print(locale.format_string('%.2f', a, grouping=True)) More on reddit.com
🌐 r/learnpython
5
1
December 8, 2021
Space as a thousands separator in the format specification mini-language - Ideas - Discussions on Python.org
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. More on discuss.python.org
🌐 discuss.python.org
1
December 1, 2024
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
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
🌐
Python
peps.python.org › pep-0378
PEP 378 – Format Specifier for Thousands Separator | peps.python.org
March 12, 2009 - The ‘,’ option indicates that ... as a thousands separator. As with locales which do not use a period as the decimal point, locales which use a different convention for digit separation will need to use the locale module to obtain appropriate formatting. The proposal works well with floats, ints, and ...
🌐
Reddit
reddit.com › r/learnpython › f-string - converting float to string with set decimals and thousand separator
r/learnpython on Reddit: f-string - converting float to string with set decimals and thousand separator
December 8, 2021 -

I'm struggling with f-strings. I want to take in a float and return string with thousand separator and two decimals, for ex. 1000.106 would return 1 000.11 as a string. I got it to work, but it looks ugly. Anyone know how to do this simpler?

code:

a = 1100300.506
b = f'{a:,.2f}'
c = b.replace(',', ' ')
print(a)
print(b)
print(c)

This will return:

1100300.506
1,100,300.51
1 100 300.51

So I will get my string like I want it, but I'm sure it's not the best way to do this.

Using this in Norway is the reason for the spaces as thousand separator.

🌐
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 - number = 4270.138 print(f"{num... should be formatted : (colon): After this the format specs will follow , (comma): Grouping separator for thousands....
🌐
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 - 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. ... Copied!my_float = 15467.3 # ✅ Format a float as currency result = f'${my_float:,.2f}' print(result) # ...
🌐
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.
Find elsewhere
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
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 ...
🌐
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 ...
🌐
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
>>> '{:,}'.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.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-a-python-string-to-float-with-thousand-separator
5 Best Ways to Convert a Python String to Float with Thousand Separator – Be on the Right Side of Change
February 19, 2024 - import locale locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8') number_string = '1,234.56' number_float = locale.atof(number_string) print(number_float) ... Here, the locale.setlocale() method configures the environment to use US number formatting. The locale.atof() method then correctly interprets the comma as a thousand separator, converting the string to a float.
🌐
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
March 6, 2024 - 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.
🌐
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 - Let’s start with the ‘Median Sales Price’ column and see how we can format it by adding the thousand comma separators and a dollar sign in the front. Below is the code that does the trick: df.loc[:, "Median Sales Price_formatted"] ='$'+ df["Median Sales Price"].map('{:,.0f}'.format) Image by Author · We use the python string format syntax '{:,.0f}'.format to add the thousand comma separators to the numbers.
🌐
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?
🌐
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.
🌐
Real Python
realpython.com › how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python – Real Python
April 24, 2024 - Here, you’ve displayed each number using scientific notation in its local form. To do this, you passed f=".2e" into format_string(). When using this function, you must precede the format with a string formatting operator symbol (%). You also set the grouping parameter to True to make sure the thousands separators were applied.
🌐
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 are using map() instead · 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.