[英]python - print iteration number without new line
我有一個循環:
for i in range(10):
print i
它打印:
1
2
...
8
9
好
但我正在尋找一條獨特的線,以實現每次迭代這樣的實現:
for i in range(10):
magic_print "this is the iteration " + i + " /10"
結果如下:
"this is the iteration <i> /10"
<i>
動態變化
謝謝 !
據我了解您的問題,您想覆蓋以前的印刷品並增加數量。 看到這個答案: https : //stackoverflow.com/a/5419488/4362607
我根據PM 2Ring的建議編輯了答案。 謝謝!
import sys
import time
def counter():
for x in range(10):
print '{0}\r'.format(x),
sys.stdout.flush()
time.sleep(1)
print
counter()
解決方案是來自str對象的format
函數
for i in range(10):
print "this is the iteration {} /10".format(i)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.