simple question

Piet van Oostrum piet at cs.uu.nl
Thu Feb 20 11:13:39 CET 2003


>>>>> "Anna" <revanna at mn.rr.com> (A) wrote:

A> 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

A> What you may not realize is that a leading zero means "octal" in python.

Only in an integer constant in a piece of Python source code. How it is
interpreted in any other context is up te the software (or person) that
interprets it. So printing "010" in some output in no way forces it to be
interpreted as octal.

A> Since I'm assuming you don't want it to be octal, you're going to have to
A> convert it to a string (rather than an integer).

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

So how is that different from print "%03d" % 10? And moreover the str is
superfluous as "0%s" % 10 already yields a string. And the %s doesn't do
much different from %d in this case. Certainly not anything that has to do
with decimal or octal.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl




More information about the Python-list mailing list