Back
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.60005>>> print('{0:04d}'.format(n)) # python >= 2.60005>>> print('{foo:04d}'.format(foo=n)) # python >= 2.60005>>> print('{:04d}'.format(n)) # python >= 2.7 + python30005>>> print('{0:04d}'.format(n)) # python 30005>>> print(f'{n:04}') # python >= 3.6
0005
Happy learning, Cheers .....!!
This example will make a string of 10 characters long, padding as necessary.
>>> t = 'test'>>> t.rjust(10, '0')>>> '000000test'
>>> t = 'test'
>>> t.rjust(10, '0')
>>> '000000test'
You can use the following video tutorials to clear all your doubts:-
31k questions
32.8k answers
501 comments
693 users