Python 3.6.5 (default, May 11 2018, 04:00:52)[GCC 8.1.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> total = 100>>> print(str(1).zfill(len(str(total))))001>>> total = 1000>>> print(str(1).zfill(len(str(total))))0001>>> total = 10000>>> print(str(1).zfill(len(str(total))))00001>>>
>python -m timeit "'{:02d}'.format(1)"1000000 loops, best of 5: 357 nsec per loop
>python -m timeit "'{0:0{1}d}'.format(1,2)"500000 loops, best of 5: 607 nsec per loop
>python -m timeit "f'{1:02d}'"1000000 loops, best of 5: 281 nsec per loop
>python -m timeit "f'{1:0{2}d}'"500000 loops, best of 5: 423 nsec per loop
>python -m timeit "str(1).zfill(2)"1000000 loops, best of 5: 271 nsec per loop
>pythonPython 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32