formatting numbers

Alex Martelli aleaxit at yahoo.com
Tue Jun 5 09:43:51 EDT 2001


"michael montagne" <montagne at boora.com> wrote in message
news:e5SS6.69668$FS3.619704 at sjc-read.news.verio.net...
> Simple question.  I am accessing a database of job numbers that take the
> form of "00049" and "01049".  These values are integers.  When I return
the

But if the leading 0's are significant, why not just take them
as the strings they are?  That would be simplest.

> value from the database it reads "49".  I need to add the leading digits
if
> they are there so this number would read "00049".  In MSAccess I used
> Format(num,"00000").  How can I do that with Python?

Several ways are available, as already mentioned in various posts,
such as:
    "%0.5d" % 49
    string.zfill(49,5)
    ("0"*5+str(49))[-5:]

but the simplest code is that which isn't there, so if you
can just keep them as string you're better off!


Alex






More information about the Python-list mailing list