1495
Comment:
|
1515
|
Deletions are marked like this. | Additions are marked like this. |
Line 45: | Line 45: |
#!python | |
Line 46: | Line 47: |
print '['+str(i)+']', # NOTE the trialing comma | print '['+str(i)+']', # NOTE the trailing comma |
Line 48: | Line 49: |
but a space will get inserted between successive prints. The 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 50: | Line 51: |
#!python | |
Line 52: | Line 54: |
sys.stdout.write('['+str(i)+'[') | sys.stdout.write('['+str(i)+']') |
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.
If you want to refer to global variables, you can replace vars() with globals().
Printing Percentages
Print strings without newlines and spaces
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
but a space will get inserted between successive prints. One way to get around this is using sys.stdout:
which will work properly.
See Also
Discussion
(None yet!)