print with no newline

Bengt Richter bokr at
Sat Sep 4 22:07:36 CEST 2004

On Fri, 03 Sep 2004 16:36:31 +0200, Peter Otten <__peter__ at> wrote:

>Paul Watson wrote:
>> I thought that using a comma at the end of a print statement would
>> suppress
>> printing of a newline.  Am I misunderstanding this feature?  How can I use
>> print and not have a newline appended at the end?
>I thought that, too. It turns out that Python writes an additional newline
>on exit if the softspace flag is set. So
>$ python -c "import sys; print 'here',; sys.stdout.softspace = False" >
>$ od -c tmp.txt
>0000000   h   e   r   e
>is a viable if ugly workaround. 
When I want printf-like control, I sometimes use (IIRC from last time ;-)

 >>> import sys
 >>> def printf(fmt, *args): sys.stdout.write(fmt % args)
 >>> printf('here')
 >>> printf('here %s\n', 'doggie')
 here doggie

Bengt Richter

More information about the Python-list mailing list