print with no newline

Bengt Richter bokr at oz.net
Sat Sep 4 16:07:36 EDT 2004


On Fri, 03 Sep 2004 16:36:31 +0200, Peter Otten <__peter__ at web.de> 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" >
>tmp.txt
>$ od -c tmp.txt
>0000000   h   e   r   e
>0000004
>
>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')
 here>>>
 >>> printf('here %s\n', 'doggie')
 here doggie
 >>>

Regards,
Bengt Richter



More information about the Python-list mailing list