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
0

3 Answers 3

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')
Sign up to request clarification or add additional context in comments.

3 Comments

Seems to doesn't work with python 3 : pastie.org/5054695
I assumed from the question that this was not python 3. let me include that.
found out that this question is a duplicate of stackoverflow.com/questions/493386/…
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

Comments

1

Use comma at the end of your first print: -

print 'Going to %s'%href, 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.