如何在一行多行打印多次?

时间:2019-06-15 16:50:51

标签: python

我知道如何在一行中循环打印:

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..

感谢帮助

1 个答案:

答案 0 :(得分:0)

您可以简单地做到:

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