user13595466
user13595466

Reputation:

Why does the same code works differently?

Well, I have read that stdout is line-buffered. But the code works differently in Pydroid 3(unaware of the exact version) and Python 3.8.3.

import time
print('Hello', end = '')
time.sleep(5)
print('World')

In Pydroid 3, both Hello and World are printed after (at least after) 5 seconds while in Python 3.8.3, Hello is printed first, and World is printed after 5 seconds.

Why is the code working differently?

Upvotes: 1

Views: 182

Answers (1)

Adam.Er8
Adam.Er8

Reputation: 13393

It is probably not a Python version issue, but a different terminal issue.

Some terminals (or more accurately files/streams, stdout included) only flush after a newline (which the first print doesn't have), while others can flush after every write.

to force a flush you can use flush=True as a param to print, try this:

import time
print('Hello', end='', flush=True)
time.sleep(5)
print('World')

Upvotes: 4

Related Questions