Python add leading zeroes using str.format [duplicate]

Solution 1:

>>> "{0:0>3}".format(1)
'001'
>>> "{0:0>3}".format(10)
'010'
>>> "{0:0>3}".format(100)
'100'

Explanation:

{0 : 0 > 3}
 │   │ │ │
 │   │ │ └─ Width of 3
 │   │ └─ Align Right
 │   └─ Fill with '0'
 └─ Element index

Solution 2:

Derived from Format examples, Nesting examples in the Python docs:

>>> '{0:0{width}}'.format(5, width=3)
'005'