Controlling output using print with format string

Alex Martelli aleaxit at yahoo.com
Mon Oct 31 01:15:01 CET 2005


Paul Watson <pwatson at redlinepy.com> wrote:

> 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?

In general, yes, because print tries hard to separate with spaces the
things it is separately printing.  You can fight that by resetting
sys.stdout.softspace after each and every print, but that's harder than
just using sys.stdout.write.  So, you should use the right tool for the
job: print for simple output, often diagnostic in nature, where you
don't mind the spaces and/or newlines that implies; sys.stdout.write
when you do want to really control every aspect of the output.


Alex



More information about the Python-list mailing list