[Tutor] print without linefeed

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 21 Nov 2001 14:27:54 -0800 (PST)


On Wed, 21 Nov 2001, Lee-Shanok, Bruce wrote:

> Hmm.. I can't for the life of me find this documented, which amazes
> me.
> 
> This is going to sound like an incredibly stupid question, but how do
> I get print to print something without automatically pasting a
> linefeed to the end? :)

Python's print statement automatically adds in a line feed without that
trailing comma that Sean talked about.  If you really want more
fine-grained control over standard output, you'll probably want to play
around with the 'sys.stdout' object:

###
>>> import sys
>>> sys.stdout("Hello World\nThis is on another line")
>>> sys.stdout.write("Hello World\nThis is on another line")
Hello World
This is on another line>>> 
###

sys.stdout behaves like a file, so most of the methods in:

    http://www.python.org/doc/lib/bltin-file-objects.html

should work with it.

Also, there's also some good information in the official Python tutorial
here about IO that has some more examples that might be interesting for
you:

    http://www.python.org/doc/current/tut/node9.html


Good luck!