qiweunvjah
qiweunvjah

Reputation: 132

Why doesn't time.sleep work as with print end arguments?

I am trying to use time.sleep() to pause in between print statements.

import time
def test():
    print("something", end="")
    time.sleep(1)
    print(" and ", end="")
    time.sleep(1)
    print("something")

When I use the end argument, the code waits before it starts to print. It should print, wait, print, wait, and print ("something, wait, and, wait, something"). However, when I use the end argument, it waits, and prints "wait something and something". This code works as I wanted it to without the end argumets.

Upvotes: 1

Views: 429

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177600

Your output device is line-buffered and only flushes when a new line is output. Add flush=True as an additional parameter to print to force a flush to the output.

Upvotes: 4

Related Questions