Asked By – Javicobos
When I use the
print "this should be"; print "on the same line"
this should be on the same line
but instead returns:
this should be
on the same line
More precisely I was trying to create a program with
if that told me whether a number was a 2 or not
def test2(x): if x == 2: print "Yeah bro, that's tottaly a two" else: print "Nope, that is not a two. That is a (x)"
But it doesn’t recognise the last
(x) as the value entered, and rather prints exactly: “(x)” (the letter with the brackets). To make it work I have to write:
print "Nope, that is not a two. That is a"; print (x)
And if e.g. I enter
test2(3) that gives:
Nope, that is not a two, that is a
So either I need to make Python recognise my (x) inside a print line as the number; or to print two separate things but on the same line.
IMPORTANT NOTE: I am using version 2.5.4
Another note: If I put
print "Thing" , print "Thing2" it says “Syntax error” on the 2nd print.
Now we will see solution for issue: Python: avoid new line with print command [duplicate]
In Python 3.x, you can use the
end argument to the
print() function to prevent a newline character from being printed:
print("Nope, that is not a two. That is a", end="")
In Python 2.x, you can use a trailing comma:
print "this should be", print "on the same line"
You don’t need this to simply print a variable, though:
print "Nope, that is not a two. That is a", x
Note that the trailing comma still results in a space being printed at the end of the line, i.e. it’s equivalent to using
end=" " in Python 3. To suppress the space character as well, you can either use
from __future__ import print_function
to get access to the Python 3 print function or use