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 ‘
’ in Python
我有一个这样的代码:
1 2 3 4 | print 'Going to %s'%href try: self.opener.open(self.url+href) print 'OK' |
当我执行它时,我会得到两行:
1 2 | Going to mysite.php OK |
但想要的是:
1 | Going to mysite.php OK |
1 2 3 4 5 6 7 8 | >>> def test(): ... print 'let\'s', ... pass ... print 'party' ... >>> test() let's party >>> |
例如:
1 2 3 4 5 6 7 | # note the comma at the end print 'Going to %s' % href, try: self.opener.open(self.url+href) print 'OK' except: print 'ERROR' |
print语句末尾的逗号指示不要添加
'
我假设这个问题是针对python 2.x的,因为
1 2 3 4 5 6 7 | # 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中,必须将
1 | print('hello', end='') |
http://docs.python.org/py3k/library/functions.html打印
在第一个
1 | print 'Going to %s'%href, |