How to fix invalid syntax error when using “print”?

2.5K    Asked by CameronOliver in Python , Asked on Jun 7, 2021

I am new to  Python and can't even write the first example:

print 'SyntaxError: invalid syntax'

this gives SyntaxError: invalid syntax


Why is this? I'm using version 3.1

Answered by Delbert Rauch

To solve invalid syntax python print you should add the parenthesis, like this:

  print()

Example is probably for the older python 2, which didn't use parentheses for print.

Reasons are that in python 2.x print is a keyword, whereas in python >3 it's a function.

For beginners, that is the biggest difference between versions 2 and 3 (the next is raw_input. For python 3 use input instead). So you can probably keep going with the examples, just remember to add the parentheses for print. Once you start printing arguments, put them inside parenthesis like this:

# The old python 2 way:
print "arg1", "arg2"
# The new python 3 way:
print("arg1", "arg2")

Your Answer

Interviews

Parent Categories