1. 使用`print()`函数和转义字符`
`:
print("第一行\n\n第二行")
输出结果:
第一行
第二行
2. 使用原始字符串(在字符串前加`r`前缀):
text = r"Line 1\n\nLine 3"
print(text)
输出结果:
Line 1
Line 3
3. 在文件操作中,使用原始字符串或特殊字符来保留空行:
text = "Line 1\n\nLine 3"
with open("output.txt", "w") as file:
file.write(text)
4. 在代码中添加额外的空行以提高可读性:
def example_function():
函数内容
example_function()
以上方法可以帮助你在Python中创建空行。选择适合你需求的方法即可