[英]How to print many times in one line, with many rows?
我知道如何在一行中循環打印:
array = [1, 2, 3, 4]
for i in array:
print("This is {}".format(i), end="")
time.sleep(1)
但是,如果我想要一個多行的輸出(帶有換行符 \\n),我該如何做這樣的事情:
print("This is {} \n And this is two {}".format(i, i + 1), end="")
time.sleep(1)
輸出將是:
This is 1
And this is two 2This is 2
And this is two 3This is 3
....
而且我要:
This is 1 #increment here in one line to 2, 3, 4..
And this is two 2 #same here 3, 4, 5..
感謝幫助
你可以簡單地做:
import time
array = [1, 2, 3, 4]
for i in range(len(array)):
if (i != len(array) - 1):
print("This is {}".format(array[i]), end="\nAnd ")
time.sleep(1)
else:
print("This is {}".format(array[i]))
輸出:
This is 1
And This is 2
And This is 3
And This is 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.