在多行中连接 python 中的字符串
Concatenate strings in python in multiline
我要连接一些字符串,结果字符串会很长。我还有一些要连接的变量。
如何组合字符串和变量,使结果成为多行字符串?
以下代码抛出错误。
str = "This is a line" +
str1 +
"This is line 2" +
str2 +
"This is line 3" ;
我也试过了
str = "This is a line" \
str1 \
"This is line 2" \
str2 \
"This is line 3" ;
请建议一种方法。
我会添加连接列表所需的所有内容,然后在换行符处加入它。
my_str = '\n'.join(['string1', variable1, 'string2', variable2])
Python 不是 php 并且您无需在变量名称前放置 $
。
a_str = """This is a line
{str1}
This is line 2
{str2}
This is line 3""".format(str1="blabla", str2="blablabla2")
有几种方法。一个简单的解决方案是添加括号:
strz = ("This is a line" +
str1 +
"This is line 2" +
str2 +
"This is line 3")
如果您希望每个 "line" 单独一行,您可以添加换行符:
strz = ("This is a line\n" +
str1 + "\n" +
"This is line 2\n" +
str2 + "\n" +
"This is line 3\n")
Python 3 使用格式化字符串的解决方案
从 Python 3.6 开始,您可以使用所谓的“格式化字符串”(或“f 字符串”)轻松地将变量插入字符串中。只需在字符串前面添加一个 f
并将变量写入花括号 ({}
) 中,如下所示:
>>> name = "John Doe"
>>> f"Hello {name}"
'Hello John Doe'
要将长字符串拆分为多行,请使用 括号 (()
) 或使用 多行字符串 [=63] =](由三个引号 """
或 '''
包围的字符串,而不是一个)。
1。解决方案:括号
在您的字符串周围加上括号,您甚至可以将它们连接起来,而无需在中间插入 +
符号:
a_str = (f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
"This is line 3") # no variable in this line, so no leading f
提示: 如果一行中没有变量,则该行不需要前导 f
。
提示: 您可以在每一行的末尾使用反斜杠 (\
) 来存档相同的结果,而不是周围的括号,但相应地 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。解决方案:多行字符串
在多行字符串中,您不需要显式插入 \n
,Python 会为您处理:
a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
提示:只要确保正确对齐代码,否则每行前面都会有前导白色 space。
顺便说一下: 你不应该调用你的变量 str
因为那是数据类型本身的名称。
格式化字符串的来源:
我要连接一些字符串,结果字符串会很长。我还有一些要连接的变量。
如何组合字符串和变量,使结果成为多行字符串?
以下代码抛出错误。
str = "This is a line" +
str1 +
"This is line 2" +
str2 +
"This is line 3" ;
我也试过了
str = "This is a line" \
str1 \
"This is line 2" \
str2 \
"This is line 3" ;
请建议一种方法。
我会添加连接列表所需的所有内容,然后在换行符处加入它。
my_str = '\n'.join(['string1', variable1, 'string2', variable2])
Python 不是 php 并且您无需在变量名称前放置 $
。
a_str = """This is a line
{str1}
This is line 2
{str2}
This is line 3""".format(str1="blabla", str2="blablabla2")
有几种方法。一个简单的解决方案是添加括号:
strz = ("This is a line" +
str1 +
"This is line 2" +
str2 +
"This is line 3")
如果您希望每个 "line" 单独一行,您可以添加换行符:
strz = ("This is a line\n" +
str1 + "\n" +
"This is line 2\n" +
str2 + "\n" +
"This is line 3\n")
Python 3 使用格式化字符串的解决方案
从 Python 3.6 开始,您可以使用所谓的“格式化字符串”(或“f 字符串”)轻松地将变量插入字符串中。只需在字符串前面添加一个 f
并将变量写入花括号 ({}
) 中,如下所示:
>>> name = "John Doe"
>>> f"Hello {name}"
'Hello John Doe'
要将长字符串拆分为多行,请使用 括号 (()
) 或使用 多行字符串 [=63] =](由三个引号 """
或 '''
包围的字符串,而不是一个)。
1。解决方案:括号
在您的字符串周围加上括号,您甚至可以将它们连接起来,而无需在中间插入 +
符号:
a_str = (f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
"This is line 3") # no variable in this line, so no leading f
提示: 如果一行中没有变量,则该行不需要前导 f
。
提示: 您可以在每一行的末尾使用反斜杠 (\
) 来存档相同的结果,而不是周围的括号,但相应地 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。解决方案:多行字符串
在多行字符串中,您不需要显式插入 \n
,Python 会为您处理:
a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
提示:只要确保正确对齐代码,否则每行前面都会有前导白色 space。
顺便说一下: 你不应该调用你的变量 str
因为那是数据类型本身的名称。
格式化字符串的来源: