使用 time.sleep 函数打印速度慢

Slow printing using time.sleep function

我想在终端上使用 python3 在同一行上缓慢打印字符串的字符。我使用了这个代码。

for i in "Hello":
    print(i,end='')
    time.sleep(0.2)

此代码等待 0.2 * 5("Hello" 的长度)秒并打印 once.when 处的所有字符 我使用 sys.stdout.write() 函数而不是打印函数,它打印字符逐行而不是同一行。我如何延迟在同一行打印字符?

import sys
import time

for c in "Hello":
    sys.stdout.write(c)
    sys.stdout.flush() # <- add this 
    time.sleep(0.2)

或者在python 3 print函数中使用flush参数

print(c, end='', flush=True)