in python how do I convert a single digit number into a double digits string?

Joe Schmoe picture Joe Schmoe · Aug 17, 2010 · Viewed 127.9k times · Source

So say i have

a = 5

i want to print it as a string '05'

Answer

jkerian picture jkerian · Aug 17, 2010

print "%02d"%a is the python 2 variant

python 3 uses a somewhat more verbose formatting system:

"{0:0=2d}".format(a)

The relevant doc link for python2 is: http://docs.python.org/2/library/string.html#format-specification-mini-language

For python3, it's http://docs.python.org/3/library/string.html#string-formatting