Controlling output using print with format string

Paul Watson pwatson at redlinepy.com
Mon Oct 31 01:44:06 CET 2005


It is clear that just using 'print' with variable names is relatively 
uncontrollable.  However, I thought that using a format string would 
reign the problem in and give the desired output.

Must I resort to sys.stdout.write() to control output?

$ python
Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
[GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> s = 'now is the time'
 >>> for c in s:
...     print c,
...
n o w   i s   t h e   t i m e
 >>> for c in s:
...     print "%c" % (c),
...
n o w   i s   t h e   t i m e
 >>> for c in s:
...     print "%1c" % (c),
...
n o w   i s   t h e   t i m e



More information about the Python-list mailing list