python 如何输出换行,Python输出换行的方法

原创
admin 3小时前 阅读数 13 #Python

Python中输出换行的方法

Python中,输出换行的方法有多种,以下是一些常见的方法:

1、使用转义字符“\n”来表示换行。

print("Hello\nWorld")

输出结果为:

Hello
World

2、使用括号内的逗号来分隔多个输出项,并在每个输出项后添加换行符。

print("Hello", "World")

输出结果为:

Hello
World

3、使用三引号来定义多行字符串,并在每个字符串后添加换行符。

print("Hello\nWorld\n")

输出结果为:

Hello
World

4、使用文件对象的write方法将字符串写入文件,并在每个字符串后添加换行符。

with open("output.txt", "w") as file:
    file.write("Hello\nWorld\n")

输出结果为:将字符串"Hello\nWorld\n"写入output.txt文件。

热门