Python创建一个长的多行字符串
在这个例子中,你将学习如何创建一个长的多行字符串。
例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:使用 \n
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.
你可以像上面的例子代码那样使用 \来写一个多行字符串。
相关文章