what's the big deal for print()

Duncan Booth duncan.booth at invalid.invalid
Mon Jun 27 04:23:30 EDT 2011


steve+comp.lang.python at pearwood.info wrote:

> Unfortunately, while that gets rid of the newline, it also leaves 
spaces
> between items:
> 
>>>> def example():
> ...     print 1,
> ...     print 2,
> ...     print 3
> ...
>>>> example()
> 1 2 3
> 
> Here's the Python 3 version:
> 
>>>> def example():
> ...     print(1, sep='', end='')
> ...     print(2, sep='', end='')
> ...     print(3, sep='')
> ...
>>>> example()
> 123
> 
> 
> To get the same result in Python 2, you have to use sys.stdout.write
().
> 

That isn't entirely true: you could set the `softspace` attribute on 
sys.stdout, but that is even messier.

>>> def foo():
...     print 1,
...     sys.stdout.softspace=0
...     print 2,
...     sys.stdout.softspace=0
...     print 3
...
>>> foo()
123

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list