我可以在Python中打印几个句子时使用time.sleep吗

时间:2019-07-08 15:34:24

标签: python-3.x printing sleep

我正在同一print()命令中打印2/3。但是我想在某些时候使用睡眠。像这样:

print("Hello world!")
time.sleep(1)
print ("I just stopped for 1 sec!!")
time.sleep(1)
print (" But I want to Marge these lines. How?")

对此或类似内容:

print ("Hello world!",time.sleep(1),"I just stopped for 1 sec!!",time.sleep(1)," But I want to Marge these lines. How?")

1 个答案:

答案 0 :(得分:1)

更改end以防止print添加换行符,并确保flush语句强制其打印,然后再继续。

import time

print("Hello world!", end=" ", flush=True)
time.sleep(1)
print ("I just stopped for 1 sec!!", end=" ", flush=True)
time.sleep(1)
print ("But I want to Marge these lines. How?")

输出:

Hello world! I just stopped for 1 sec!! But I want to Marge these lines. How?