在多行中连接python中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34295901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate strings in python in multiline
提问by user3290349
I have some strings to be concatenate and the resultant string will be quite long. I also have some variables to be concatenated.
我有一些字符串要连接,结果字符串会很长。我还有一些要连接的变量。
How can I combine both strings and variables so the result would be a multi line string?
如何组合字符串和变量,以便结果是多行字符串?
The following code throws error.
以下代码抛出错误。
str = "This is a line" +
str1 +
"This is line 2" +
str2 +
"This is line 3" ;
I have tried this too
我也试过这个
str = "This is a line" \
str1 \
"This is line 2" \
str2 \
"This is line 3" ;
Please suggest a way to do this.
请提出一种方法来做到这一点。
采纳答案by Bryan Oakley
There are several ways. A simple solution is to add parenthesis:
有几种方法。一个简单的解决方案是添加括号:
strz = ("This is a line" +
str1 +
"This is line 2" +
str2 +
"This is line 3")
If you want each "line" on a separate line you can add newline characters:
如果您希望每个“行”在单独的行上,您可以添加换行符:
strz = ("This is a line\n" +
str1 + "\n" +
"This is line 2\n" +
str2 + "\n" +
"This is line 3\n")
回答by Du?an Ma?ar
I would add everything I need to concatenate to a list and then join it on a line break.
我会添加我需要连接到列表的所有内容,然后在换行符处加入它。
my_str = '\n'.join(['string1', variable1, 'string2', variable2])
回答by Diogo Martins
Python isn't php and you have no need to put $
before a variable name.
Python 不是 php,您无需$
在变量名之前放置。
a_str = """This is a line
{str1}
This is line 2
{str2}
This is line 3""".format(str1="blabla", str2="blablabla2")
回答by winklerrr
Solutions for Python 3 using Formatted Strings
使用格式化字符串的 Python 3 解决方案
As of Python 3.6you can use so called "formatted strings" (or "f strings") to easily insert variables into your strings. Just add an f
in front of the string and write the variable inside curly braces ({}
) like so:
从Python 3.6 开始,您可以使用所谓的“格式化字符串”(或“f 字符串”)轻松地将变量插入到您的字符串中。只需f
在字符串前面添加一个并将变量写入大括号 ( {}
) 中,如下所示:
>>> name = "John Doe"
>>> f"Hello {name}"
'Hello John Doe'
To split a long string to multiple lines surround the parts with parentheses(()
) or use a multi-line string(a string surrounded by three quotes """
or '''
instead of one).
要将长字符串拆分为多行,请使用括号( ()
) 或使用多行字符串(由三个引号"""
或'''
代替一个引号包围的部分)。
1. Solution: Parentheses
1. 解决方案:括号
With parentheses around your strings you can even concatenate them without the need of a +
sign in between:
在字符串周围加上括号,您甚至可以将它们连接起来,而无需在它们+
之间添加符号:
a_str = (f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
"This is line 3") # no variable here, so no leading f
Good to know:If there is no variable in a line, there is no need for a leading f
for that line.
很高兴知道:如果一行中没有变量,则该行不需要前导f
。
Good to know:You could archive the same result with backslashes (\
) at the end of each line instead of surrounding parentheses but accordingly to PEP8you should prefer parentheses for line continuation:
很高兴知道:您可以\
在每行末尾使用反斜杠 ( ) 而不是环绕括号来存档相同的结果,但因此,对于PEP8,您应该更喜欢使用括号作为行的延续:
Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
通过将表达式括在括号中,可以将长行分成多行。这些应该优先于使用反斜杠进行行延续。
2. Solution: Multi-Line String
2. 解决方案:多行字符串
In multi-line strings you don't need to explicitly insert \n
, Python takes care of that for you:
在不需要显式插入的多行字符串中\n
,Python 会为您处理:
a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
Good to know:Just make sure you align your code correctly otherwise you will have leading white space in front each line.
很高兴知道:只要确保正确对齐代码,否则每行前面都会有前导空格。
By the way:you shouldn't call your variable str
because that's the name of the datatype itself.
顺便说一句:您不应该调用您的变量,str
因为那是数据类型本身的名称。
Sources for formatted strings:
格式化字符串的来源: