tl;dr; Use f"{number:,}"
to thousands format an integer to a string.
I keep forgetting and having to look this up every time. Hopefully by blogging about it, this time it'll stick in my memory. And hopefully in yours too :)
Suppose you have a number, like 1234567890
and you want to display it, here's how you do it:
>>> number = 1234567890
>>> f"{number:,}"
'1,234,567,890'
In the past, before Python 3.6, I've been using:
>>> number = 1234567890
>>> format(number, ",")
'1,234,567,890'
All of this and more detail can be found in PEP 378 -- Format Specifier for Thousands Separator. For example, you can do this beast too:
>>> number = 1234567890
>>> f"{number:020,.2f}"
'0,001,234,567,890.00'
which demonstrates (1) how to do zero-padding (of length 20), (2) the thousands comma, (3) round to 2 significant figures. All useful weapons to be able to draw from the top of your head.
UPDATE
Also, incredibly useful is the equivalent of somestring.ljust(10)
:
>>> mystr = "peter"
>>> f"{mystr:10}"
'peter '
>>> f"{mystr:>10}"
' peter'
Comments
Post your own commentYou can also format numbers with an underscore separator instead of commas (`format(number, '_')`). That has the advantage of being both quickly human-eye-parsable AND python parsable, e.g. you can copy and paste the output right back into the REPL (and quite a few other languages also understand numbers with underscore separators).
Also, since the variable *named* underscore is the last result shown in the REPL, you can reformat the last output with underscore separators by typing `f"{_:_}"`, which is silly and confusing syntax but often pretty convenient.
Cheers! This is a good point. Practical.
I love it.
I tried the following, it printed with none of the single quotes.
num = int(input("Please enter number:")) # Input a number, using int as integer
print(f"{num: ,}") # Without the single quotes
===========================
Please enter number: 1234567890
===========================
1,234,567,890
"none of the single quotes"?? What do you mean?
>>> number = 1234567890
>>> f"{number:,}"
'1,234,567,890'
======================
This came from your example,
do you see the single quotes.
======================
' 1,234,567,890'
When using the print cmd,
you don't get those as indicated in the
previous post.
The single quotes come from the __repr__ method. If you do `print(repr('some string'))` the output becomes: 'some string'
But if you do just `print('some string')` the output becomes: some string
See https://docs.python.org/3/library/functions.html#repr
In the Python interactive shell if you just type a variable or expression and hit enter, it will automatically do `print(repr(thing))` for you.
I.e.:
>>> thing = 'string'
>>> thing
'string'
I've been looking for a reasonable way to do this. Thanks.
This is very helpful, thank you for sharing this! I had a relatively complicated method using a function I created to handle this. These options are so much simpler!
And what about if I want to use dot as thousands separator? How can I achieve this?
Thank you.