Fixed width number formatting python 3

Viewed 76320
3 Answers

Better:

number=12
print(f'number is equal to {number:03d}')

There is built-in string method .zfill for filling 0-s:

>>> str(42).zfill(5)
'00042'
>>> str(42).zfill(2)
'42'
Related