欢迎访问宙启技术站
智能推送

Python的print()函数怎么样打印换行和不换行

发布时间:2023-07-01 15:41:57

在Python中,可以通过使用print()函数来打印输出文本。下面是打印换行和不换行的方法:

换行打印:

print("Hello, World!")
print("This is a new line.")

运行上述代码,会得到以下输出:

Hello, World!
This is a new line.

不换行打印:

print("Hello, World!", end=" ")
print("This is on the same line.")

运行上述代码,会得到以下输出:

Hello, World! This is on the same line.

可以注意到,在第二个print()语句中,我们使用了end=" "参数来指定打印结束后的字符。默认情况下,end参数的值为

,即换行符。通过改变end参数的值,可以控制打印结果是否换行。

此外,还可以使用转义字符

来手动创建换行:

print("Hello, World!
This is a new line.")

运行上述代码,会得到与 个例子相同的输出结果:

Hello, World!
This is a new line.

这只是介绍了print()函数的基本用法,实际中还可以利用其它参数和格式化字符串来更好地控制打印输出。