Python add leading zeroes using str.format

Can you display an integer value with leading zeroes using the str.format function?

Example input:


Desired output:


I know that both zfill and %-based formatting (e.g. '%03d' % 5) can accomplish this. However, I would like a solution that uses str.format in order to keep my code clean and consistent (I'm also formatting the string with datetime attributes) and also to expand my knowledge of the Format Specification Mini-Language.

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


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


Back to homepage or read more recommendations: