Here is bad but simple solution if you don't want to mess with locale:
'{:,}'.format(1234567890.001).replace(',', ' ')
Answer from Raz on Stack OverflowHere 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(",", " ")
Videos
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.
If you are only dealing with integers, you can use:
x = 123456789
'{:,}'.format(x).replace(',','.')
# returns
'123.456.789'
and if you want to add some decimals (e.g.: 2), you can just chain a few replace operations:
x = 123456789.123456789
'{:,.2f}'.format(x).replace(',','*').replace('.', ',').replace('*','.')
>>> '123.456.789,12'