print([object,...][,seq=' '][,<a href="https://www.gaodaima.com/tag/end" title="查看更多关于end的文章" target="_blank">end</a>=' '][,file=sys.stdout])
www#gaodaima.com来源gaodai#ma#com搞@@代~&码网搞代码
我们在使用print()函数时,并不希望输出结束后自动换行,因此,我们可以按照下面的方法来做
1.print()指定结束符
print('hello',end='') print('world') #result:helloworld
搞代码网,免费学习python!
当print()函数,指定end参数为空字符后,print()函数就不再主动添加换行符了。并且,hello和world之间也不存在任何空格。
a = 'first line' b = 'second line' c = 'third line' print(a,end=' ') print(b) print(c,end='!')
我们可以利用指定结束符的方法,灵活控制换行行数和结尾字符。
2.print()函数
知道了如何实现输出不换行,下面我们来看一下原理。
print()函数的形式是:
print(*objects, sep=' ', end=' ', file=sys.stdout,flush=False) objects -- 复数,表示可以一次输出多个对象。输出多个对象时,需要用 , 分隔。 sep -- 用来间隔多个对象,默认值是一个空格。 end -- 用来设定以什么结尾。默认值是换行符 ,我们可以换成其他字符串。 file -- 要写入的文件对象。 flush--是否要强行刷新stream
来源:搞代码网:原文地址:https://www.gaodaima.com