在Python中,为什么没有换行符就无法打印出来? [英] In Python, why won't something print without a newline?
本文介绍了在Python中,为什么没有换行符就无法打印出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import time
import sys
sys.stdout.write("1")
time.sleep(5)
print("2")
5秒后将打印"12"
will print "12" after 5 seconds
import time
import sys
sys.stdout.write("1\n")
time.sleep(5)
print("2")
立即打印"1 \ n",然后在5秒钟后打印"2"
will print "1\n" right away, then "2" after 5 seconds
这是为什么?
推荐答案
如果添加"\ n",则流将自动刷新,并且结尾处也没有换行符. 您可以使用以下命令刷新输出:
If you add "\n" then stream is flushed automaticaly, and it is not without new line at the end. You can flush output with:
sys.stdout.flush()
这篇关于在Python中,为什么没有换行符就无法打印出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文