如何用多行打印输出
var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29
print(100*(var1+var2))
在Python中的上述代码中,我将输出作为单线输出 output
我希望它被多行打印。我该怎么做才能使它起作用?我尝试使用\ n逃脱字符,但没有获得所需的输出。
var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29
print(100*(var1+var2))
in the above code in python, I am getting the output as a single-line
output
I want it to be printed in multiple lines. what should I do to make it work?. I have tried using the \n escape character but I am not getting the required output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用循环的
,而不是字符串乘法,如果您的意图是100次打印同一件事;它会消耗较少的内存(不是100倍6个字符的字符串,但有了更大的数字,它可能会变得很重要),并且对休闲读者来说,意图更为明显:
如果您真的想构造一个除了插入
\ n
之外,在其中有Newlines的单字符串:另一个选项是将
str.join
与列表乘法使用:生成器表达式:
或 上面的任何一个都带有
sep
参数print()
:I'd suggest using a
for
loop rather than string multiplication if your intent is to print the same thing 100 times; it'll consume less memory (not that 100 times a 6-character string is a lot, but with a larger number it could become significant), and it makes the intent more obvious to the casual reader:If you really wanted to construct a single string with newlines in it, apart from inserting
\n
before multiplying:another option would be to use
str.join
with list multiplication:or a generator expression:
or to do either of the above with the
sep
parameter inprint()
:添加
\ n
字符:Add
\n
character:根据您提到的代码,以下是输出:
根据我对问题的理解,预期的输出如下:
要获取上述输出,请在代码中使用“ \ n”,如下所示:
如果您还需要在下一行中打印“ 86”,可以使用以下代码等等:
并期望以下输出:
Based on the code mentioned by you, below is the output:
As per my understanding of the question, the output expected is as below:
To get the above output, please use "\n" in your code as shown below:
Also, if you need "86" also to be printed in next line, you can use the below code and so on:
And expect below output: