python add zeros before number code example
Example 1: python convert number to string with leading zeros
str(1).zfill(2)
str(23).zfill(4)
Example 2: python add zero to string
>>> n = '4'
>>> print(n.zfill(3))
004
Example 3: python3 format leading 0
print(f"{1:02d}")
Example 4: python add zero to string
>>> n = 4
>>> print(f'{n:03}')
004
>>> print('%03d' % n)
004
>>> print(format(n, '03'))
004
>>> print('{0:03d}'.format(n))
004
>>> print('{foo:03d}'.format(foo=n))
004
>>> print('{:03d}'.format(n))
004
Example 5: how to append leading zeros in python
str.zfill(width)
Example 6: python3 format leading 0
print("{:02d}".format(1))