python2和python3 print输出不换行

python2 print不换行

在print最后加上一个逗号,会把两个输出打印在同一行,不过两个输出之间有一个空格的间隔,例如:

print '{0}'.format(123),
print '{0}'.format(456)

输出:

123 456

如果没有逗号:

print '{0}'.format(123)
print '{0}'.format(456)

输出:

123
456

python3 print不换行

python3中print函数中的参数end默认值为'\n',表示换行,给end赋值为空,就不会换行了,例如:

print (123,end='')
print (456,end='')

输出:

123456


python2 print不换行另一种实现

python2在文件首行加上 from __future__ import print_function ,也可以使用python3中给end参数赋空(值)的方式实现输出不换行:

from __future__ import print_function
print (123,end='')
print (456,end='')

输出:
123456

【转】https://blog.csdn.net/dcrmg/article/details/79091926

 

猜你喜欢

转载自www.cnblogs.com/sggggr/p/12006858.html