print fmstr % str <= possible??

Bjorn Pettersen BPettersen at NAREX.com
Mon Jan 21 20:07:39 EST 2002


> From: Ozone Hole near South Pole [mailto:ozonehole2k at yahoo.com] 
> 
> 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?

It would probably help if you gave us the error message, but I'm
assuming you're missing the format specifier in your myprint calls, i.e.
they should be:

  myprint("The x pos is %2.3f", x) 
  myprint("The y pos is %5.3f", y) 

Instead (notice the extra 'f').

-- bjorn




More information about the Python-list mailing list