How can I show the output of two print statements on the same line?
开发者_Go百科I have 2 separate print statements:
print "123"
print "456"
How can i make these 2 print statement appear on the same line? Note i need to use 2 print statements
output:
123456
In python 1.x and 2.x, a trailing comma will do what you want (with the caveat mentioned by others about the extra space inserted):
print "123",
print "456"
In python 3.x — or in python 2.6-2.7 with from __future__ import print_function
— print
is a function and you should use end=""
to force no extra ending character:
print("123", end="")
print("456")
A comma after the string you are printing will suppress the newline. The \b
is a special character that represents an ASCII backspace.
print '123',
print '\b456'
print "123",
print "456"
Alternatively, you can use sys.stdout.write
:
sys.stdout.write('123')
sys.stdout.write('456\n')
print '123',
print '\b456'
print "123",
print "456"
For Python 3+
print("123", end="")
print("456")
精彩评论