Fix Python – Invalid syntax when using “print”? [duplicate]

Question

Asked By – Lonnie Price

I’m learning Python and can’t even write the first example:

print 2 ** 100

this gives SyntaxError: invalid syntax

pointing at the 2.

Why is this? I’m using version 3.1

Now we will see solution for issue: Invalid syntax when using “print”? [duplicate]


Answer

That is because in Python 3, they have replaced the print statement with the print function.

The syntax is now more or less the same as before, but it requires parens:

From the “what’s new in python 3” docs:

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

This question is answered By – TM.

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0