printing unfinished line

Joshua Macy amused at webamused.com
Sat Mar 25 16:39:06 EST 2000


Gang Seong Lee wrote:
> 
> Is there any way that I can see the output of a string not having a new line
> char.
> For example,
> 
> import time
> 
> print 'abc',
> time.sleep(3)
> print 'def'
> 
> 'abc' is not displayed on the screen until it encounters last print 'def',
> which has new line at the end of the line. sys.write('abc') has the same
> effect.
> 
> Is it possible I can see 'abc' before it goes to print 'def' ?
> 
> Thank you
> 
> Gang


  Others have answered your question about flushing the buffer, but as
far as printing without a newline, use a comma at the end of the print
statement:
>>> print "Hello"; print "World"
Hello
World
>>> print "Hello", ; print "World"   # note the comma after "Hello"
Hello World
>>> 

Also note that the comma does insert a space (the second example printed
"Hello World", not "HelloWorld").  If you need it without the space,
you'll have to do something else, for instance:
>>> print "%s%s" % ("Hello", "World")
HelloWorld

Joshua



More information about the Python-list mailing list