简体   繁体   English

如何换行打印?

[英]How to print in newline?

var1 = "54"
var4 = "32"
print(100 * str(int(var1) + int(var4)))

Above, this is my code.以上,这是我的代码。 can anyone tell me how to print the result 100 times in a new line?谁能告诉我如何在新行中打印 100 次结果? I tried many times using \n and with end keyword but I got errors and errors only.我尝试了很多次使用 \n 和 end 关键字,但我只得到错误和错误。

Use a for loop:使用for循环:

var1 = '54'
var4 = '32'
for i in range(100):
    print(int(var1) + int(var4)) # prints 86

I think you are trying.. for this我认为你正在尝试..为此

var1 = "54"
var4 = "32"
print(100 * (str(int(var1) + int(var4))+str("\n"))) #This will print 86 100 times in new line

you can also use for loop for your desired output.您还可以对所需的 output 使用 for 循环。

var1 = "54"
var4 = "32"

for i in range(100):
    print(int(var1) + int(var4)) #Same Output

You can apply while loop also but when we know to iterate till specific value we should use for .您也可以应用while loop ,但是当我们知道要迭代到特定值时,我们应该使用for

There is no need of end="\n" cause every time when for loop iterate print statement came into new line不需要end="\n"因为每次 for 循环迭代打印语句进入新行时

If you wanted your data not in a new line but with a space or like tab than you will need to use end ex:如果您希望您的数据不在新行中,而是带有空格或类似制表符,则需要使用end ex:

print(int(var1) + int(var4),end=" ") This will print 86 with one space like. print(int(var1) + int(var4),end=" ")这将用一个空格打印 86。 86 86 86... 100times 86 86 86... 100次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM