How to print one character at a time on one line?

Solution 1:

Two tricks here, you need to use a stream to get everything in the right place and you also need to flush the stream buffer.

import time
import sys

def delay_print(s):
    for c in s:
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(0.25)

delay_print("hello world")

Solution 2:

Here's a simple trick for Python 3, since you can specify the end parameter of the print function:

>>> import time
>>> string = "hello world"
>>> for char in string:
    print(char, end='')
    time.sleep(.25)


hello world

Have fun! The results are animated now!

Solution 3:

import sys
import time

string = 'hello world\n'
for char in string:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(.25)