formatting numbers

Ken Seehof kens at sightreader.com
Mon Jun 4 17:11:34 EDT 2001


>>> num = 49
>>> print "%05d" % num
00049

The % operator is popular for C/C++ programmers like me, who are
used to the standard library C function "printf".  Otherwise you may
prefer a somewhat more pythonic approach:

>>> print ("0000" + `num`)[-5:]  # works for num >= 0
00049


----- Original Message -----
From: "michael montagne" <montagne at boora.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Monday, June 04, 2001 1:21 PM
Subject: formatting numbers


> 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
> 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?
>
> -mjm
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20010604/20741d7b/attachment.html>


More information about the Python-list mailing list