Revision 3 as of 2004-07-28 05:38:01

Clear message

String Formatting

The [http://docs.python.org/lib/typesseq-strings.html section of the manual on String Formatting Operations] is hidden in the section on [http://docs.python.org/lib/typesseq.html Sequence types.]

Direct Variable Reference

A common trick you can use when writing strings is to refer directly to variables.

   1 a = "hello, world!"
   2 x = 34
   3 y = 96
   4 
   5 print """
   6 a = %(a)s
   7 x,y = (%(x)s,%(y)s)
   8 """ % vars() # local variables

If you want to refer to global variables, you can replace vars() with globals().

Printing Percentages

   1 percent = lambda x:"%2.2f%%" % x
   2 
   3 print percent(35.3567) # prints "35.35%"

See Also

EscapingHtml, WorkingWithTime

Discussion

Unable to edit the page? See the FrontPage for instructions.