[英]How can I print many pre-existing strings separated by a line?
I'm working on a project and I have a bunch of already defined strings with various names我正在做一个项目,我有一堆已经定义好的不同名称的字符串
I want to print all of them, each one starting on a new line.我想打印所有这些,每一个都从一个新行开始。 What I have right now is this:我现在拥有的是这样的:
print(line1 + '\n' + line2 + '\n' + line3 ... )
Don't get me wrong, it works, but I was just wondering if there was an easier way to do it that would physically shorten the line.不要误会我的意思,它有效,但我只是想知道是否有更简单的方法可以在物理上缩短线路。 Thanks!谢谢!
You can use the .join
method:您可以使用.join
方法:
>>> line1 = "a"
>>> line2 = "b"
>>> line3 = "c"
>>> "\n".join([line1, line2, line3])
'a\nb\nc'
>>> print("\n".join([line1, line2, line3]))
a
b
c
Or, you can use the sep
parameter of print
if you don't need to store the string:或者,如果不需要存储字符串,可以使用print
的sep
参数:
>>> print(line1, line2, line3, sep="\n")
a
b
c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.