I want to print characters of a string slowly on the terminal on the same line using python3. i used this code.
for i in "Hello":
print(i,end='')
time.sleep(0.2)
this code waits for 0.2 * 5(len of "Hello") secs and prints all the characters at once.when i use sys.stdout.write() function instead of print function, it prints the characters line by line instead of same line. how do i print characters on same line with delay?
import sys
import time
for c in "Hello":
sys.stdout.write(c)
sys.stdout.flush() # <- add this
time.sleep(0.2)
Or use the flush
parameter in the python 3 print
function
print(c, end='', flush=True)