首页 / 知识

pythonprint如何不换行

2023-11-12 13:12:00

大家应该知道python中print之后是默认换行的,

那如何我们不想换行,且不想讲输出内容用一个print函数输出时,就需要改变print默认换行的属性,

方法如下:

print('contents',end='!@#$%^&*')

end就表示print将如何结束,默认为end="\n"(换行)

栗子:

print("祝各位身体健康")

print("!")

print("祝各位身体健康",end='')

print("!")

python3.0的print函数有如下的形式:

print([object,...][,seq=''][,end='\n'][,file=sys.stdout])

我们在使用print()函数时,并不希望输出结束后自动换行,因此,我们可以按照下面的方法来做

1.print()指定结束符

print('hello',end='')

print('world')

#result:helloworld

当print()函数,指定end参数为空字符后,print()函数就不再主动添加换行符了。并且,hello和world之间也不存在任何空格。

a='firstline'

b='secondline'

c='thirdline'

print(a,end='\n\n')

print(b)

print(c,end='!')

我们可以利用指定结束符的方法,灵活控制换行行数和结尾字符。

以上内容为大家介绍了python培训之print如何不换行,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们http://www.mobiletrain.org/


培训灵活空格结尾函数

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢