[Tutor] Trying to write a string formatting demo

Dick Moores rdm at rcblue.com
Sun Apr 10 09:38:14 CEST 2005


I'm trying to write a string formatting demo, just for things like 
%.4f,  %.3e, and %.3g.

Here's what I have, but of course it doesn't work. What should the print 
statement be?

========================
x = raw_input("enter a float: ")
if x == "":
     print "x will be the default 1234.56789"
     x = 1234.56789
else:
     x = float(x)

s = raw_input("enter a float format string such as '.4f' or '.3e': ")

print "%f formatted with '%s' is %s" % (x,s,x)
==========================

Thanks,

Dick Moores
rdm at rcblue.com



More information about the Tutor mailing list