Display number with leading zeros

In Python 2 (and Python 3) you can do:

number = 1
print("%02d" % (number,))

Basically % is like printf or sprintf (see docs).

For Python 3.+, the same behavior can also be achieved with format:

number = 1

For Python 3.6+ the same behavior can be achieved with f-strings:

number = 1

Leave a Comment