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
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.

🌐
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.

🌐
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 ...
🌐
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.
🌐
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
By including a comma in the f-string format specifier, you can automatically format numbers with thousands separators. ... Here, the f-string is constructed with {number:,} to include the comma as a thousand separator.
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
peps.python.org › pep-0378
PEP 378 – Format Specifier for Thousands Separator | peps.python.org
It is expected that some other solution will arise for specifying alternative separators. ... Scanning the web, I’ve found that thousands separators are usually one of COMMA, DOT, SPACE, APOSTROPHE or UNDERSCORE.
🌐
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.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-format-number-thousands-separator-2-decimals
Format number with comma as thousands separator in Python | bobbyhadz
Use a formatted string literal to format a number with a comma as the thousands separator to 2 decimals, e.g. `result = f'{my_float:,.2f}'`.
🌐
Sololearn
sololearn.com › en › Discuss › 3235225 › thousand-separator-for-float-nums
thousand separator for float nums? | Sololearn: Learn to code for FREE!
number = 4270.138 print(f"{num... should be formatted : (colon): After this the format specs will follow , (comma): Grouping separator for thousands....
🌐
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 ...
🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
Use a comma as a thousand separator: price = 59000 txt = f"The price is {price:,} dollars" print(txt) Try it Yourself » · Here is a list of all the formatting types. Before Python 3.6 we used the format() method to format strings.
🌐
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.
🌐
YouTube
youtube.com › pygpt
python f string format thousand separator - YouTube
Download this code from https://codegive.com Title: Formatting Numbers with Thousand Separators Using Python f-stringsIntroduction:In Python, f-strings provi...
Published   December 19, 2023
Views   6
🌐
YouTube
youtube.com › codestack
python f string thousand separator - YouTube
Download this code from https://codegive.com In Python, f-strings provide a concise and readable way to embed expressions inside string literals. Starting fr...
Published   December 14, 2023
Views   2
🌐
Python Guides
pythonguides.com › python-format-number-with-commas
How To Format Numbers With Commas In Python?
January 16, 2025 - 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) In this example, the colon (:) followed by a comma (,) inside the curly braces instructs Python to format the number with commas as ...
🌐
Python Morsels
pythonmorsels.com › string-formatting
Python f-string tips & cheat sheets - Python Morsels
April 12, 2022 - And the n format specifier formats a number in a locale-aware way (using period, comma, or another appropriate thousands separator based on the locale): >>> import locale >>> locale.setlocale(locale.LC_NUMERIC, "en_IN.utf-8") 'en_IN.utf-8' >>> print(f"The population peaked at {population:n}.") The population peaked at 9,67,72,25,658. >>> locale.setlocale(locale.LC_NUMERIC, "en_US.utf-8") 'en_US.utf-8' >>> print(f"The population peaked at {population:n}.") The population peaked at 9,677,225,658. Python also has format specifiers for representing numbers in binary or hexadecimal.
🌐
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.
🌐
GitHub
github.com › micropython › micropython › issues › 11284
f-string thousands separator doesn't work with floats · Issue #11284 · micropython/micropython
March 8, 2023 - On v1.19.1, f-string thousands separator doesn't work with floats. Tested on both unix and rp2040: Demo code: foo_int = 123456 foo_float = 1234.56789 print(f"{foo_int:,} {foo_float:,}") CPython (correct): 123,456 1,234.56789 MicroPython ...
Published   Apr 17, 2023