动态打印一行 [英] Print in one line dynamically
本文介绍了动态打印一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做几个给出标准输出的语句,而不会在语句之间看到换行符.
I would like to make several statements that give standard output without seeing newlines in between statements.
具体来说,假设我有:
for item in range(1,100):
print item
结果是:
1
2
3
4
.
.
.
如何让它看起来像:
1 2 3 4 5 ...
更好的是,是否可以在最后一个数字上打印单个数字,这样屏幕上一次只有一个数字?
Even better, is it possible to print the single number over the last number, so only one number is on the screen at a time?
推荐答案
将 print item
更改为:
打印项目,
Python 2.7print(item, end=" ")
在 Python 3 中
print item,
in Python 2.7print(item, end=" ")
in Python 3
如果要动态打印数据,请使用以下语法:
If you want to print the data dynamically use following syntax:
print(item, sep=' ', end='', flush=True)
在 Python 3 中
print(item, sep=' ', end='', flush=True)
in Python 3
这篇关于动态打印一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文