In Python 2.7 and 3.x you can use str.format for this:
>>> num = 1234567890.0876543
>>> "{0:,f}".format(num)
'1,234,567,890.087654'
>>> "{0:,.2f}".format(num)
'1,234,567,890.08'
>>> "{0:,f}".format(1234)
'1,234.000000'
Answer from Ashwini Chaudhary on Stack OverflowIn Python 2.7 and 3.x you can use str.format for this:
>>> num = 1234567890.0876543
>>> "{0:,f}".format(num)
'1,234,567,890.087654'
>>> "{0:,.2f}".format(num)
'1,234,567,890.08'
>>> "{0:,f}".format(1234)
'1,234.000000'
I don't think you looked deep enough into the locale module. locale.format() is what you want, but make sure you set a locale first or you won't get grouping at all.
>>> locale.setlocale(locale.LC_ALL, '')
'en_US.UTF-8'
>>> locale.format("%.4f", 12345.678912, grouping=True)
'12,345.6789'
Let's say I have 2792819, now I want it to be like 2,792,819. How do I do it?
I can do so while reversing it and after every 3 iterations, put a comma or something like that. But is there a better way to do so?
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.
Your own example is able to do what you want. Just write it properly without mixing format styles:
a = 1234567
print "The number is {:,d} but it's converted to a string".format(a)
That's a good reason to use the modern syntax. It does not care about the type of the argument. It will use str on the object and output it accordingly.
print "The number is {:,d} and John has {} sisters and {} brothers".format(1000000, 2, 3)
To be clear, a itself is not being changed into a string. There is a temporary, anonymous string object created by '{:,d}'.format(a) and then fed to "%s" %:
>>> a = 1234567
>>> "The number is %s but it's converted to a string" %'{:,d}'.format(a)
"The number is 1,234,567 but it's converted to a string"
>>> type(a)
<type 'int'>
>>> a
1234567
So a itself has not changed at all similarly as your second example does not change the object b:
>>> b = 0.1234
>>> "The Norwegian Blue prefers kippin' on it's back! %0.2f%%" % (b*100)
"The Norwegian Blue prefers kippin' on it's back! 12.34%"
>>> type(b)
<type 'float'>
>>> b
0.1234
So the underlying values of a and b are not changing. The issue is that you are using a less than clear syntax to print a. Just do:
>>> "The number is {:,d} but it's NOT converted to a string".format(a)
"The number is 1,234,567 but it's NOT converted to a string"
There is no need to do two formatting steps as you have in your example.