print and trailing white space

Peter Otten __peter__ at web.de
Thu Sep 18 18:15:50 EDT 2003


Maxime Biais wrote:

> I know how to print a string without the trailing newline with a coma
> like :
> 
> print "hello word",
> 
> but this example add a white space after the string :(
> 
> Do you know how to avoid this white space without using write()
> 

>>> class Out(object):
...     def _set_softspace(self, value):
...             pass
...     def _get_softspace(self):
...             return False
...     softspace = property(_get_softspace, _set_softspace)
...     def write(self, s):
...             sys.stdout.write(s)
...
>>> out = Out()
>>> print >> out, "a", "b", "c"
abc
>>> print >> out, "x",
x>>>

It's possible, but I'd rather go with write().
And, please, never ever substitute sys.stdout with a similar object.

Peter





More information about the Python-list mailing list