Python 中可使用以下三种方法换行写代码:1. 在行末使用反斜杠 (\);2. 使用括号 ();3. 使用分号 (;)。三重引号 ("""或''') 可定义多行字符串。

Python 中换行写代码
在 Python 中,使用反斜杠(\)字符可以在代码行末尾换行。
以下方式可以换行写代码:
-
在代码行末尾使用反斜杠 (\)
立即学习“Python免费学习笔记(深入)”;
print("This is line 1 \\ and this is line 2.") -
使用括号 ()
print("This is line 1" "and this is line 2.") -
使用分号 (;)
print("This is line 1" + " and this is line 2.")
示例:
# 以下是换行写代码的示例:
print("This is a" \
" multiline string.")
# 以下输出将是一个多行字符串:
This is a
multiline string.注意:
- 使用反斜杠换行时,需要在反斜杠后面直接跟一个换行符。
- 使用括号或分号时,换行符必须出现在代码行的末尾。
- 多行字符串也可以使用三重引号 ("""或''') 来定义。











