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.
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?
Print Number with Commas in Python - Ask a Question - TestMu AI Community
Format Integer With Comma Using Python Printf
Can anyone help me with this problem?
python - Is there a way to format a number with commas for thousands, without converting the int to a string? - Stack Overflow
Videos
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.
No one so far has mentioned the new ',' option which was added in version 2.7 to the Format Specification Mini-Language -- see PEP 378: Format Specifier for Thousands Separator in the What's New in Python 2.7 document. It's easy to use because you don't have to mess around with locale (but is limited for internationalization due to that, see the original PEP 378). It works with floats, ints, and decimals — and all the other formatting features provided for in the mini-language spec.
Sample usage:
print format(1234, ",d") # -> 1,234
print "{:,d}".format(1234) # -> 1,234
print(f'{1234:,d}') # -> 1,234 (Python 3.6+)
Note: While this new feature is definitely handy, it's actually not all that much harder to use the locale module, as several others have suggested. The advantage is that then numeric output can be made to automatically follow the proper thousands (and other) separator conventions used in various countries when outputting things like numbers, dates, and times. It's also very easy to put the default settings from your computer into effect without learning a bunch of language and country codes. All you need to do is:
import locale
locale.setlocale(locale.LC_ALL, '') # empty string for platform's default settings
After doing that you can just use the generic 'n' type code for outputting numbers (both integer and float). Where I am, commas are used as the thousand separator, so after setting the locale as shown above, this is what would happen:
print format(1234, "n") # -> 1,234
print "{:n}".format(1234) # -> 1,234
Much of the rest of the world uses periods instead of commas for this purpose, so setting the default locale in many locations (or explicitly specifying the code for such a region in a setlocale() call) produces the following:
print format(1234, "n") # -> 1.234
print "{:n}".format(1234) # -> 1.234
Output based on the 'd' or ',d' formatting type specifier is unaffected by the use (or non-use) of setlocale(). However the 'd' specifier is affected if you instead use the locale.format() or locale.format_string() functions.
locale.format()
Don't forget to set the locale appropriately first.