simple question

Anna revanna at mn.rr.com
Tue Feb 18 00:32:51 EST 2003


On Mon, 17 Feb 2003 14:40:52 +0000, les wrote:

> consider this,
> 
>>>> print str "%-3d" % 10
> 10
>
> how do I get python to print "010" instead?
> 
> thanks

What you may not realize is that a leading zero means "octal" in python.
Since I'm assuming you don't want it to be octal, you're going to have to
convert it to a string (rather than an integer).

>>> print str ("0%s" % 10)
010
>>> 

If you want it to always be three characters, regardless of the target,
you'll need to use zero padding.

>>> print str ("%03d" % 10)
010
>>> print str ("%03d" % 8)
008
>>> 

You can tell Python how wide you want the string to be. For example,
if you want the string to be 5 characters wide:

>>> print str("%05d" % 10)
00010
>>>

Oh, and if, by chance you *did* want octal?

>>> x = 8
>>> print oct(x)
010
>>> 

>>> print "\nJust my", str("%.2f" % .03), "worth...\nAnna"

Just my 0.03 worth...
Anna





More information about the Python-list mailing list