[英]python - print iteration number without new line
I have a loop: 我有一个循环:
for i in range(10):
print i
and it print : 它打印:
1
2
...
8
9
OK 好
but I'm searching to make a unique line which actualize for each iteration like this : 但我正在寻找一条独特的线,以实现每次迭代这样的实现:
for i in range(10):
magic_print "this is the iteration " + i + " /10"
with a result like: 结果如下:
"this is the iteration <i> /10"
<i>
changing dynamically <i>
动态变化
Thanks ! 谢谢 !
As I understand your question, you would like to overwrite previous prints and count up. 据我了解您的问题,您想覆盖以前的印刷品并增加数量。 See this answer: https://stackoverflow.com/a/5419488/4362607
看到这个答案: https : //stackoverflow.com/a/5419488/4362607
I edited the answer according to PM 2Ring's suggestions. 我根据PM 2Ring的建议编辑了答案。 Thanks!
谢谢!
import sys
import time
def counter():
for x in range(10):
print '{0}\r'.format(x),
sys.stdout.flush()
time.sleep(1)
print
counter()
the solution is the format
function from the str object 解决方案是来自str对象的
format
函数
for i in range(10):
print "this is the iteration {} /10".format(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.