一般来说, 一行就是一条语句, 但有时语句过长不利于阅读, 一般会写成多行的形式, 这时需要在换行时使用反斜杠: \
name = "Lilei"
age = 23
gender = "boy"
str = "Hello, my name is " + \
name + \
". I'm " + \
str(age) + \
"years old " + \
gender
print(str)
# Hello, my name is Lilei. I'm 23years old boy
如果语句换行时包裹在中括号: [], 大括号: {}, 或者括号: (), 中, 则不必反斜杠换行, 如下:
name = "Lilei"
age = 23
gender = "boy"
str = ("Hello, my name is " +
name +
". I'm " +
str(age) +
"years old " +
gender)
print(str)