【发布时间】:2013-05-08 10:49:46
【问题描述】:
假设我想通过 2 个不同的打印语句打印 'hello'。
喜欢:
print "hel"
print "lo"
但是python会自动打印新行,所以如果我们希望它在同一行中,我们需要 -
print "hel"**,**
这就是问题所在 - , 创造了一个空间,我希望它连接起来。谢谢。
【问题讨论】:
假设我想通过 2 个不同的打印语句打印 'hello'。
喜欢:
print "hel"
print "lo"
但是python会自动打印新行,所以如果我们希望它在同一行中,我们需要 -
print "hel"**,**
这就是问题所在 - , 创造了一个空间,我希望它连接起来。谢谢。
【问题讨论】:
你可以使用print函数
>>> from __future__ import print_function
>>> print('hel', end=''); print('lo', end='')
hello
显然分号在代码中只是为了在交互式解释器中显示结果。
print 函数的 end 关键字参数指定要在正文之后打印的内容。默认为'\n'。这里我们把它改成'',这样最后就什么都不打印了。
【讨论】:
>>> import sys
>>> sys.stdout.write('hel');sys.stdout.write('lo')
hello
【讨论】:
from __future__ import print_function。
print 语句,所以至少有了这个我不需要使用该函数