Reputation: 3557
I know how to utilize time.sleep()
, but I was curious how to print something like so:
"hey...(pause)...you...(pause)....there"
where the 'pause' is some time.sleep()
interval. I can only print these intervals on separate lines. Is there a way to keep it all on one line?
Upvotes: 0
Views: 349
Reputation: 309929
In a print
statement in python2.x, you can suppress the newline with a trailing comma:
print "hey...",
time.sleep(1)
print "...you...",
time.sleep(1)
print "....there"
On python3.x (or with from __future__ import print_function
enabled), you use the end
keyword to the print
function:
print("hey...", end="")
etc.
Or, finally, you can always1 write to the sys.stdout
stream:
import sys
sys.stdout.write("hey...")
time.sleep(1)
...
The advantage here is that you can flush it explicitly:
sys.stdout.flush()
1technically not always. sys.stdout
could be replaced by something else: sys.stdout = 3
-- But making a mess without cleaning up after yourself is terribly rude. However, if you ever find yourself in that situation, sys.__stdout__
is a backup ;-).
Upvotes: 1
Reputation: 82470
In python 2:
print "hey...",
time.sleep(0.5)
In python 3:
print("hey...", end=' ')
time.sleep(0.5)
Upvotes: 3