python如何让两个print函数不分行

更新时间:02-10 综合 由 浅殇 分享

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 = 'first line'

b = 'second line'

c = 'third line'

print(a,end='nn')

print(b)

print(c,end='!')

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

2.print()函数

知道了如何实现输出不换行,下面我们来看一下原理。

print()函数的形式是:print(*objects, sep=' ', end='n', file=sys.stdout,flush=False)

objects -- 复数,表示可以一次输出多个对象。输出多个对象时,需要用 , 分隔。

ep -- 用来间隔多个对象,默认值是一个空格。

end -- 用来设定以什么结尾。默认值是换行符 n,我们可以换成其他字符串。

file -- 要写入的文件对象。

flush--是否要强行刷新stream

声明:关于《python如何让两个print函数不分行》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/all/15_1937432.html