victorlin
victorlin

Reputation: 704

Why is there no delay between same-line printing?

print 'foo',
time.sleep(1)
print 'bar'

This seems to run time.sleep(1) first, then prints "foo bar" all at once.

However, printing both foo and bar on its own lines produces the expected delay between the print statements:

print 'foo'
time.sleep(1)
print 'bar'

Is there something that stacks all print statements until a new line character is received?

Upvotes: 3

Views: 157

Answers (1)

desiato
desiato

Reputation: 1152

print by default prints to sys.stdout and it is line-buffered. you could flush the buffer each time after a print statement

import time
import sys

print 'foo'
sys.stdout.flush()
time.sleep(1)
print 'bar

reference: sys

read also: How to flush output of Python print?

Upvotes: 7

Related Questions