Chris
Chris

Reputation: 28064

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

Upvotes: 2

Views: 8807

Answers (4)

Kanishka Gupta
Kanishka Gupta

Reputation: 11

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

print("write your code")

Upvotes: -1

miku
miku

Reputation: 187994

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

Upvotes: 4

Nikola Smiljanić
Nikola Smiljanić

Reputation: 26873

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

Upvotes: 11

eduffy
eduffy

Reputation: 40214

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

print (recip)

Upvotes: 3

Related Questions