It's easy just use this,
For Strings:
>>> n = '5'
>>> print(n.zfill(4))
0005
For numbers:
>>> n = 5
>>> print('%04d' % n)
0005
>>> print(format(n, '05')) # python >= 2.6
0005
>>> print('{0:04d}'.format(n)) # python >= 2.6
0005
>>> print('{foo:04d}'.format(foo=n)) # python >= 2.6
0005
>>> print('{:04d}'.format(n)) # python >= 2.7 + python3
0005
>>> print('{0:04d}'.format(n)) # python 3
0005
>>> print(f'{n:04}') # python >= 3.6
0005
Happy learning, Cheers .....!!