'Why is this invalid syntax?

Why is this complaining about an invalid syntax?

#! /usr/bin/python

recipients = []
recipients.append('[email protected]')

for recip in recipients:
    print recip

I keep getting:

File "send_test_email.py", line 31
    print recip
              ^
SyntaxError: invalid syntax


Solution 1:[1]

If you are using Python 3 print is a function. Call it like this: print(recip).

Solution 2:[2]

In python 3, print is no longer a statement, but a function.

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

More python 3 print functionality:

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

Solution 3:[3]

If it's Python 3, print is now a function. The correct syntax would be

print (recip)

Solution 4:[4]

in python 3,you have to use parenthesis with print function ..wriite like this :-

print("write your code")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SilentGhost
Solution 2
Solution 3 eduffy
Solution 4