在Python中,编写多行代码主要有以下几种方法:
使用反斜杠(\)
使用反斜杠进行换行
a = 'this is line number 1\
this is line number 2\
this is line number 3\
this is line number 4'
print(a)
使用括号(圆括号、方括号或花括号)
使用括号括起来
a = ('this is line number 1', 'this is line number 2', 'this is line number 3', 'this is line number 4')
print(a)
使用三引号(单引号或双引号)
使用三引号创建多行字符串
multi_line_string = """this is line number 1
this is line number 2
this is line number 3
this is line number 4"""
print(multi_line_string)
以上方法可以帮助你编写整洁、易读的多行代码。需要注意的是,在字符串中使用反斜杠时,如果不想在字符串中包含换行符,可以在行尾添加反斜杠。