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.

🌐
Finxter
blog.finxter.com › python-string-to-float-with-comma-easy-conversion-guide
Python String to Float with Comma: Easy Conversion Guide – Be on the Right Side of Change
import csv import locale locale.setlocale(locale.LC_ALL, 'your_locale_here') with open('data.csv', newline='') as csvfile: reader = csv.reader(csvfile) for row in reader: your_data = locale.atof(row[0]) # Convert first column to float · In machine learning with Python, libraries like NumPy and SciPy expect data to be in numeric format. If your data includes strings with commas as decimal separators, convert them before feeding to your model:
🌐
Python Guides
pythonguides.com › python-format-number-with-commas
How To Format Numbers With Commas In Python?
January 16, 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, ...
🌐
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
Comma (,) Both (may vary by location or other factors) Arabic decimal separator (٫) Data unavailable Map by NuclearVacuum on Wikipedia · What people often do when interpreting those numbers with Python is simply using the replace method of the str class. In [1]: number = '12,75' In [2]: parsed = float(number.replace(',', '.')) In [3]: parsed Out[3]: 12.75 ·
🌐
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 - Here, the X variable is assigned with value, and after printing the type() function, we get the output as a float type. For more information on data types, please read this article. The f-string is considered a ‘formatted string’ in python. The syntax of the f-string is very easy; it begins with the f letter and ends with the curly braces. 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-with-comma-to-float-in-python
Convert String with Comma To Float in Python - GeeksforGeeks
July 23, 2025 - In this example, below Python code converts the numeric string "1,234.56" to a float by removing the comma and then prints the result.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-format-number-thousands-separator-2-decimals
Format number with comma as thousands separator in Python | bobbyhadz
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
peps.python.org › pep-0378
PEP 378 – Format Specifier for Thousands Separator | peps.python.org
The ‘,’ option indicates that commas should be included in the output 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...
🌐
GeeksforGeeks
geeksforgeeks.org › print-number-commas-1000-separators-python
Print number with commas as 1000 separators in Python - GeeksforGeeks
May 14, 2025 - 2 min read Convert String with Comma To Float in Python · When working with data in Python, it's not uncommon to encounter numeric values formatted with a mix of commas and dots as separators. Converting such strings to float is a common task, and Python offers several simple methods to achieve ...
🌐
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 ... be formatted : (colon): After this the format specs will follow , (comma): Grouping separator for thousands....
🌐
CopyProgramming
copyprogramming.com › howto › how-to-format-a-float-with-a-comma-as-decimal-separator-in-an-f-string
Python: Formatting a Float Value in an f-string with Comma as Decimal Separator: A Guide
April 17, 2023 - While working with Python, I aim to transform float variables into string variables with a decimal comma and two decimal places. But since Excel in the Netherlands employs a comma as a decimal separator, I'd like the "position" outcome in the text-file to be in the format of 123,456.
Top answer
1 of 10
210

Using the localization services

The default locale

The standard library locale module is Python's interface to C-based localization routines.

The basic usage is:

import locale
locale.atof('123,456')

In locales where , is treated as a thousands separator, this would return 123456.0; in locales where it is treated as a decimal point, it would return 123.456.

However, by default, this will not work:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/locale.py", line 326, in atof
    return func(delocalize(string))
ValueError: could not convert string to float: '123,456'

This is because by default, the program is "in a locale" that has nothing to do with the platform the code is running on, but is instead defined by the POSIX standard. As the documentation explains:

Initially, when a program is started, the locale is the C locale, no matter what the user’s preferred locale is. There is one exception: the LC_CTYPE category is changed at startup to set the current locale encoding to the user’s preferred locale encoding. The program must explicitly say that it wants the user’s preferred locale settings for other categories by calling setlocale(LC_ALL, '').

That is: aside from making a note of the system's default setting for the preferred character encoding in text files (nowadays, this will likely be UTF-8), by default, the locale module will interpret data the same way that Python itself does (via a locale named C, after the C programming language). locale.atof will do the same thing as float passed a string, and similarly locale.atoi will mimic int.

Using a locale from the environment

Making the setlocale call mentioned in the above quote from the documentation will pull in locale settings from the user's environment. Thus:

>>> import locale
>>> # passing an empty string asks for a locale configured on the
>>> # local machine; the return value indicates what that locale is.
>>> locale.setlocale(locale.LC_ALL, '')
'en_CA.UTF-8'
>>> locale.atof('123,456.789')
123456.789
>>> locale.atof('123456.789')
123456.789

The locale will not care if the thousands separators are in the right place - it just recognizes and filters them:

>>> locale.atof('12,34,56.789')
123456.789

In 3.6 and up, it will also not care about underscores, which are separately handled by the built-in float and int conversion:

>>> locale.atof('12_34_56.789')
123456.789

On the other side, the string format method, and f-strings, are locale-aware if the n format is used:

>>> f'{123456.789:.9n}' # `.9` specifies 9 significant figures
'123,456.789'

Without the previous setlocale call, the output would not have the comma.

Setting a locale explicitly

It is also possible to make temporary locale settings, using the appropriate locale name, and apply those settings only to a specific aspect of localization. To get localized parsing and formatting only for numbers, for example, use LC_NUMERIC rather than LC_ALL in the setlocale call.

Here are some examples:

>>> # in Denmark, periods are thousands separators and commas are decimal points
>>> locale.setlocale(locale.LC_NUMERIC, 'en_DK.UTF-8')
'en_DK.UTF-8'
>>> locale.atof('123,456.789')
123.456789
>>> # Formatting a number according to the Indian lakh/crore system:
>>> locale.setlocale(locale.LC_NUMERIC, 'en_IN.UTF-8')
'en_IN.UTF-8'
>>> f'{123456.789:9.9n}'
'1,23,456.789'

The necessary locale strings may depend on your operating system, and may require additional work to enable.

To get back to how Python behaves by default, use the C locale described previously, thus: locale.setlocale(locale.LC_ALL, 'C').

Caveats

Setting the locale affects program behaviour globally, and is not thread safe. If done at all, it should normally be done just once at the beginning of the program. Again quoting from documentation:

It is generally a bad idea to call setlocale() in some library routine, since as a side effect it affects the entire program. Saving and restoring it is almost as bad: it is expensive and affects other threads that happen to run before the settings have been restored.

If, when coding a module for general use, you need a locale independent version of an operation that is affected by the locale (such as certain formats used with time.strftime()), you will have to find a way to do it without using the standard library routine. Even better is convincing yourself that using locale settings is okay. Only as a last resort should you document that your module is not compatible with non-C locale settings.

When the Python code is embedded within a C program, setting the locale can even affect the C code:

Extension modules should never call setlocale(), except to find out what the current locale is. But since the return value can only be used portably to restore it, that is not very useful (except perhaps to find out whether or not the locale is C).

(N.B: when setlocale is called with a single category argument, or with None - not an empty string - for the locale name, it does not change anything, and simply returns the name of the existing locale.)

So, this is not meant as a tool, in production code, to try out experimentally parsing or formatting data that was meant for different locales. The above examples are only examples to illustrate how the system works. For this purpose, seek a third-party internationalization library.

However, if the data is all formatted according to a specific locale, specifying that locale ahead of time will make it possible to use locale.atoi and locale.atof as drop-in replacements for int and float calls on string input.

2 of 10
205

Just remove the , with replace():

float("123,456.908".replace(',',''))
🌐
Bobby Hadz
bobbyhadz.com › blog › python-convert-string-with-comma-separator-and-dot-to-float
Python: Convert string with comma separator and dot to float | bobbyhadz
Copied!my_str = '456.789,4567' result = float(my_str.replace('.', '').replace(',', '.')) print(result) # 👉️ 456789.4567 · The first call to the replace method removes the period . from the string. The second call replaces the comma with a period. You can learn more about the related topics by checking out the following tutorials: Count number of unique Words in a String or File in Python
🌐
ActiveState
code.activestate.com › recipes › 577054-comma-float-to-float
"Comma float" to float « Python recipes « ActiveState Code
Convert comma separated floating point number (12,3456) to float. ... My bad, I did not cover all you test cases. This is better: float(float_string.replace(".","").replace(",",".")) ... There is already string.atof and locale.atof for handling different decimals points. "1,234.567.890" is not be a number so this should fail any conversion from a formatted ...