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:
Answer from Ian Schneider on Stack OverflowThe
'_'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.
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.
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.
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.
Videos
Here is bad but simple solution if you don't want to mess with locale:
'{:,}'.format(1234567890.001).replace(',', ' ')
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(",", " ")