Differences between revisions 2 and 3
Revision 2 as of 2004-07-28 05:37:00
Size: 806
Editor: dsl254-010-130
Comment:
Revision 3 as of 2004-07-28 05:38:01
Size: 823
Editor: dsl254-010-130
Comment:
Deletions are marked like this. Additions are marked like this.
Line 36: Line 36:
EscapingHtml EscapingHtml, WorkingWithTime

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

  • (none yet!)

StringFormatting (last edited 2008-11-15 14:00:43 by localhost)

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