python – Invalid syntax when using print?

python – Invalid syntax when using print?

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 whats 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)!

You need parentheses:


python – Invalid syntax when using print?

They changed print in Python 3. In 2 it was a statement, now it is a function and requires parenthesis.

Heres the docs from Python 3.0.

Leave a Reply

Your email address will not be published. Required fields are marked *