print fmstr % str <= possible??

Dale Strickland-Clark dale at riverhall.NOTHANKS.co.uk
Mon Jan 21 20:09:41 EST 2002


ozonehole2k at yahoo.com (Ozone Hole near South Pole) wrote:

>Hi,
>
>If I want to print some numerical data, I need to:
>print "The answer is %2.3f" % ans
>
>Is that possible to pass a fmtstr to do something like this
>def myprint(fmtstr,ans):
>  print fmtstr % ans
>
>myprint("The x pos is %2.3", x) 
>myprint("The y pos is %5.3", y) 
>
>
>I know the above will not work.  What's the correct way to do this?

Did you try it?

>>> print "Number is %2.3f" % 1.123
Number is 1.123
>>> 

your second example will work, if you put the format character in the
string:

myprint("The x pos is %2.3f", x) 
myprint("The y pos is %5.3f", y) 
here----------------------^
--
Dale Strickland-Clark
Riverhall Systems Ltd



More information about the Python-list mailing list