how to print string at the end of the previous string Python
Possible Duplicate:
How to print in Python without newline or space?
How to print a string without including 'n' in Python
I have a code that looks like this:
print 'Going to %s'%href
try:
self.opener.open(self.url+href)
print 'OK'
When I execute it I get two lines obviously:
Going to mysite.php
OK
But want is :
Going to mysite.php OK
>>> def test():
... print 'let's',
... pass
... print 'party'
...
>>> test()
let's party
>>>
for your example:
# note the comma at the end
print 'Going to %s' % href,
try:
self.opener.open(self.url+href)
print 'OK'
except:
print 'ERROR'
the comma at the end of the print statement instructs to not add a 'n'
newline character.
I assumed this question was for python 2.x because print
is used as a statement. For python 3 you need to specify end=''
to the print function call:
# note the comma at the end
print('Going to %s' % href, end='')
try:
self.opener.open(self.url+href)
print(' OK')
except:
print(' ERROR')
In python3 you have to set the end
argument (that defaults to n
) to empty string:
print('hello', end='')
http://docs.python.org/py3k/library/functions.html#print
在第一次print
结束时使用逗号: -
print 'Going to %s'%href,
链接地址: http://www.djcxy.com/p/77214.html
上一篇: 删除字符串之间的空格