3

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
Community
  • 1
  • 1
Vor
  • 33,215
  • 43
  • 135
  • 193

3 Answers3

5
>>> 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')
dnozay
  • 23,846
  • 6
  • 82
  • 104
2

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

etuardu
  • 5,066
  • 3
  • 46
  • 58
1

Use comma at the end of your first print: -

print 'Going to %s'%href, 
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525