You can use print() method with the format to pad any characters to a string in Python as shown below –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
s = "5" print(s.zfill(3)) n = 5 print(f'{n:03}') # Preferred method, python >= 3.6 print('%03d' % n) print(format(n, '03')) # python >= 2.6 print('{0:03d}'.format(n)) # python >= 2.6 + python 3 print('{foo:03d}'.format(foo=n)) # python >= 2.6 + python 3 print('{:03d}'.format(n)) # python >= 2.7 + python3 ## Returns 005 |