Given:
a = 1
b = 10
c = 100
How do I display a leading zero for all numbers with less than two digits?
This is the output I\'m expe
In Python >= 3.6, you can do this succinctly with the new f-strings that were introduced by using:
f'{val:02}'
which prints the variable with name val
with a fill value of 0
and a width of 2
.
For your specific example you can do this nicely in a loop:
a, b, c = 1, 10, 100
for val in [a, b, c]:
print(f'{val:02}')
which prints:
01
10
100
For more information on f-strings, take a look at PEP 498 where they were introduced.