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
To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3. With Python 3, you do have the added flexibility of changing the end argument to print on the same line.
Print on same line with some sign between elementsThe end=”,” is used to print in the same line with a comma after each element. We can use some other sign such as '. ' or ';' inside the end parameter.
To print on the same line in Python, add a second argument, end=' ', to the print() function call.
You can use the cat() function to easily print multiple variables on the same line in R. This function uses the following basic syntax: cat(variable1, variable2, variable3, ...) The following examples show how to use this syntax in different scenarios.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With