温馨提示×

python print方法如何控制换行

小樊
81
2024-09-25 06:40:11
栏目: 编程语言

在Python中,可以使用print()函数的end参数来控制换行。默认情况下,print()函数会在输出内容后添加一个换行符,使得下一次输出内容出现在新的一行。

如果需要在同一行输出多个内容,可以将end参数设置为空字符串'',这样print()函数就不会添加换行符。例如:

print("Hello, world!", end='')
print("Welcome to Python!")

输出结果为:

Hello, world!Welcome to Python!

如果需要在每个内容后面都添加换行符,可以将end参数设置为换行符\n。例如:

print("Hello, world!\nWelcome to Python!")

输出结果为:

Hello, world!
Welcome to Python!

0