how can I output text to the console without new line at the end? for example:
print 'temp1'
print 'temp2'
output:
temp1
temp2
And I need:
temp1temp2
how can I output text to the console without new line at the end? for example:
print 'temp1'
print 'temp2'
output:
temp1
temp2
And I need:
temp1temp2
Add a comma after the last argument:Alternatively, Call
sys.stdout.write
:In Python > 2.6 and Python 3:
Try this:
There are multiple ways, but the usual choice is to use
sys.stdout.write()
, which -- unlikeprint
-- prints exactly what you want. In Python 3.x (or in Python 2.6 withfrom __future__ import print_function
) you can also useprint(s, end='', sep='')
, but at that pointsys.stdout.write()
is probably easier.Another way would be to build a single string and print that:
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.)
Try