
Python thousands separators - Don't use commas and use underscores
You can format an Python interger as a string with thousands separators using the format()
.
s1 = '{:,}'.format(31415)
s2 = '{:,}'.format(123456789)
s3 = '{:,}'.format(10000000000)
print(s1) # 31,415
print(s2) # 123,456,789
print(s3) # 10,000,000,000
The expression of Python intergers with thousands separators
a = 31415
b = 1, 000
c = 123, 456, 789
print(a) # 31415
print(b) # (1, 0)
print(c) # (123, 456, 789)
Python integers can't contain separator commas and 1, 000
is regarded as a tuple of (1, 0). A Comma works as a separator. An underscore works as thousands separators.
b = 1_000
c = 123_456_789
print(b) # 1000
print(c) # 123456789
print(type(b)) # <class 'int'>
print(type(c)) # <class 'int'>
The underscores don't necessarily separate thousands.
b = 10_00
c = 1234_56789
print(b) # 1000
print(c) # 123456789
print(type(b)) # <class 'int'>
print(type(c)) # <class 'int'>
Comments
Powered by Markdown