python输出如何不换行

原创
ithorizon 7个月前 (10-02) 阅读数 41 #Python

Python输出不换行的方法

在Python中,如果想要输出内容并且不换行,可以使用print()函数,将end参数设置为'',表示输出内容后不换行。

print('Hello, world!', end='')
print('This is a second line!', end='')

输出结果为:

Hello, world!This is a second line!

可以看到,两行输出内容之间并没有换行。

如果想要在输出内容之前也不换行,可以在print()函数中添加sep参数,表示输出内容之间的分隔符。

print('Hello, world!', 'This is a second line!', sep='')

输出结果为:

Hello, world!This is a second line!

同样,两行输出内容之间并没有换行,注意,sep参数只有在输出多个内容时才需要使用。



热门