示例 1:使用三引号
my_string = '''The only way to
learn to program is
by writing code.'''
print(my_string)
输出
The only way to learn to program is by writing code.
您可以使用 '''(多行字符串)'''
或 """(多行字符串)"""
来打印多行字符串,如上所示。
示例 2:使用括号和单引号/双引号
my_string = ("The only way to \n"
"learn to program is \n"
"by writing code.")
print(my_string)
输出
The only way to learn to program is by writing code.
如果您使用 (" ")
语法,则需要使用 \n
显式指定换行符。
示例 3:使用 \
my_string = "The only way to \n" \
"learn to program is \n" \
"by writing code."
print(my_string)
输出
The only way to learn to program is by writing code.
您可以使用 \
,如上面的示例代码所示,来编写多行字符串。