No newline using printf

Peter Hansen peter at engcorp.com
Thu Sep 15 22:47:41 EDT 2005


Johnny Lee wrote:
> Roy Smith wrote:
> 
>>For closer control over output, use the write() function.  You want
>>something like:
>>
>>import sys
>>for i in range(3):
>>   sys.stdout.write (str(i))
> 
> 
> here is the output of my machine:
> 
>  >>> import sys
>  >>> for i in range(3):
>  ...     sys.stdout.write(str(i))
>  ...
>  012>>>
> 
> Why the prompt followed after the output? Maybe it's not as expected.

Because, unlike print, sys.stdout.write() just sends the raw bytes 
directly to the output without special formatting, extra characters 
(such as the newline print adds for you), or other interference.

Add the newline yourself after the loop to fix this:

sys.stdout.write('\n')

-Peter



More information about the Python-list mailing list