动态打印一行
问题描述:
我想做几个给出标准输出的语句,而不会在语句之间看到换行符.
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.7 -
print(item, end=" ")
在 Python 3 中
-
print item,
in Python 2.7 -
print(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