Display number with leading zeros
You can use the str.format()
method to display a number with leading zeros in Python. The {:0Xd}
format specifier, where X is the total width of the number including leading zeros and d stands for decimal, can be used to pad the number with leading zeroes.
For example, to display the number 5 with leading zeroes to make it 2 digits wide, you can use the following code:
This will output 05
.
You can also use f-strings (Python 3.6+)
This will also output 05
.