left padding zeroes on a string...

cjl cjlesh at gmail.com
Fri Mar 25 23:13:50 CET 2005


Hey all:

I want to convert strings (ex. '3', '32') to strings with left padded
zeroes (ex. '003', '032'), so I tried this:

string1 = '32'
string2 = "%03s" % (string1)
print string2

>32

This doesn't work. If I cast string1 as an int it works:

string1 = '32'
int2 = "%03d" % (int(string1))
print int2

>032

Of course, I need to cast the result back to a string to use it. Why
doesn't the first example work?

-cjl




More information about the Python-list mailing list