Given:
a = 1
b = 10
c = 100
I want to display a leading zero for all numbers with less than 2 digits.
Essentially displaying
01
10
100
Given:
a = 1
b = 10
c = 100
I want to display a leading zero for all numbers with less than 2 digits.
Essentially displaying
01
10
100
x = [1, 10, 100]
for i in x:
print '%02d' % i
results:
01
10
100
Read more information about string formatting using % in the documentation.
Here you are:
print "%02d" % (1,)
Basically % is like printf or sprintf.
Use a format string - http://docs.python.org/lib/typesseq-strings.html
For example:
python -c 'print "%(num)02d" % {"num":5}'
You can use zfill:
print str(1).zfill(2)
print str(10).zfill(2)
print str(100).zfill(2)
prints:
01
10
100