Differences between revisions 11 and 12
Revision 11 as of 2005-10-29 14:40:33
Size: 1858
Editor: c-24-6-47-52
Comment:
Revision 12 as of 2005-10-30 08:38:35
Size: 2032
Editor: FredDrake
Comment:
Deletions are marked like this. Additions are marked like this.
Line 43: Line 43:
The standard print statement automatically inserts newlines. This can be overcome with The standard `print` statement automatically inserts newlines. This can be overcome with
Line 49: Line 49:
but a space will get inserted between successive prints. One way to get around this is using sys.stdout: but a space will get inserted between successive prints. One way to get around this is using `sys.stdout`:
Line 65: Line 65:
You can also print a backspace ('\b') to swallow the extra space. You can also print a backspace (`'\b'`) to swallow the extra space.
Line 68: Line 68:
#!python
Line 73: Line 74:
Unfortunately, this produces a mess if output is being directed to a file;
it's best to avoid printing the space if you don't want it.
Line 74: Line 78:
I want to print out all member of "sys", what I need is I want to print out all member of `sys`, then what I need is
Line 76: Line 80:
#!python
Line 77: Line 82:
    print "sys.", member, "->", eval("sys.%s" % member)     print "sys." + member, "->", repr(getattr(sys, member))

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%"

You may find it tricky to print out a feed of numbers (as output from within a loop) on one line, without being separated by a space. An example could be output such as

Processing...
[1][2][3][4][5][6]
Completed.

The standard print statement automatically inserts newlines. This can be overcome with

   1 for i in range(10):
   2     print '['+str(i)+']',  # NOTE the trailing comma

but a space will get inserted between successive prints. One way to get around this is using sys.stdout:

   1 import sys
   2 for i in range(10):
   3     sys.stdout.write('['+str(i)+']')

which will work properly.

See Also

EscapingHtml, WorkingWithTime

Discussion

You can also print a backspace ('\b') to swallow the extra space. eg:

   1 for i in range(10):
   2     print '\b%d' % i,
   3 # Prints: 0123456789

Unfortunately, this produces a mess if output is being directed to a file; it's best to avoid printing the space if you don't want it.

Variable name substitution using format and eval

I want to print out all member of sys, then what I need is

   1 for member in dir(sys):
   2     print "sys." + member, "->", repr(getattr(sys, member))

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

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