我有一个看起来像这样的代码:
print 'Going to %s'%href
try:
self.opener.open(self.url+href)
print 'OK'
当我执行它时,我明显得到两行:
Going to mysite.php
OK
但想要的是:
Going to mysite.php OK
我有一个看起来像这样的代码:
print 'Going to %s'%href
try:
self.opener.open(self.url+href)
print 'OK'
当我执行它时,我明显得到两行:
Going to mysite.php
OK
但想要的是:
Going to mysite.php OK
>>> def test():
... print 'let\'s',
... pass
... print 'party'
...
>>> test()
let's party
>>>
对于您的示例:
# note the comma at the end
print 'Going to %s' % href,
try:
self.opener.open(self.url+href)
print 'OK'
except:
print 'ERROR'
print 语句末尾的逗号指示不要添加'\n'
换行符。
我认为这个问题是针对 python 2.x 的,因为print
它被用作语句。对于 python 3,您需要指定end=''
print 函数调用:
# note the comma at the end
print('Going to %s' % href, end='')
try:
self.opener.open(self.url+href)
print(' OK')
except:
print(' ERROR')
在 python3 中,您必须将end
参数(默认为\n
)设置为空字符串:
print('hello', end='')
在你的第一个结尾使用逗号print
: -
print 'Going to %s'%href,