views:

178

answers:

4

Hello, how can I output text to the console without new line at the end? for example:

print 'temp1'
print 'temp2'

->

temp1

temp2

And I need:

temp1temp2

+1  A: 

Try this:

print 'temp1',
print 'temp2'
Andrew Hare
Beat me by two seconds... :(
SLaks
The comma will still introduce a space between the strings.
Thomas Wouters
Note that this will still add a space between test1 and test2, which is not what the OP said he needed.
jemfinch
+4  A: 
SLaks
The comma will still introduce a space between the strings.
Thomas Wouters
+2  A: 

There are multiple ways, but the usual choice is to use sys.stdout.write(), which -- unlike print -- prints exactly what you want. In Python 3.x (or in Python 2.6 with from __future__ import print_function) you can also use print(s, end='', sep=''), but at that point sys.stdout.write() is probably easier.

Another way would be to build a single string and print that:

>>> print "%s%s" % ('temp1', 'temp2')

But that obviously requires you to wait with writing until you know both strings, which is not always desirable, and it means having the entire string in memory (which, for big strings, may be an issue.)

Thomas Wouters
+2  A: 

On python 2.6:

from __future__ import print_function

print('temp1', end='')
print('temp2', end='')
Olivier