Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print underscore separated integer

Since python3.6, you can use underscore to separate digits of an integer. For example

x = 1_000_000
print(x)  #1000000

This feature was added to easily read numbers with many digits and I found it very useful. But when you print the number you always get a number not separated with digits. Is there a way to print the number with its digits separated with underscore.

P.S. I want the output as integer not as string. Not "1_000_000" but 1_000_000

like image 742
Aven Desta Avatar asked Mar 03 '26 12:03

Aven Desta


2 Answers

Try using this:

>>> x = 1_000_000
>>> print(f"{x:_}")
1_000_000

Here are details

Another way would be to use format explicitly:

>>> x = 1_000_000
>>> print(format(x, '_d'))
1_000_000
like image 132
Sayandip Dutta Avatar answered Mar 05 '26 01:03

Sayandip Dutta


print('{:_}'.format(x))

Output:

1_000_000
like image 43
Zaraki Kenpachi Avatar answered Mar 05 '26 00:03

Zaraki Kenpachi