python format string zero pad

Solutions on MaxInterview for python format string zero pad by the best coders in the world

showing results for - "python format string zero pad"
María Alejandra
31 Jan 2017
1>>> n = '4'
2>>> print(n.zfill(3))
3# 004
4>>> n = 4
5>>> print(f'{n:03}') # Preferred method, python >= 3.6
6#004
7>>> print('%03d' % n)
8#004
9>>> print(format(n, '03')) # python >= 2.6
10#004
11>>> print('{0:03d}'.format(n))  # python >= 2.6 + python 3
12#004
13>>> print('{foo:03d}'.format(foo=n))  # python >= 2.6 + python 3
14#004
15>>> print('{:03d}'.format(n))  # python >= 2.7 + python3
16#004