2

Why is this complaining about an invalid syntax?

#! /usr/bin/python

recipients = []
recipients.append('chris@elserinteractive.com')

for recip in recipients:
    print recip

I keep getting:

File "send_test_email.py", line 31
    print recip
              ^
SyntaxError: invalid syntax
David Locke
  • 17,926
  • 9
  • 33
  • 53
Chris
  • 27,596
  • 25
  • 124
  • 225

4 Answers4

11

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

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Nikola Smiljanić
  • 26,745
  • 6
  • 48
  • 60
  • I'm guessing python 3.0 has a lot of breaking changes? Seems like raw_input no longer works in favor of input too. Hard for a beginner to follow examples when they're in incompatible versions. :( – Chris Dec 07 '09 at 17:43
  • All 3.x changes are listed in "What's new" sections of 3.0 and 3.1 docs. – Cat Plus Plus Dec 07 '09 at 17:47
  • If you're learning from examples based on 2.6, you should be using 2.6. It's still available. – recursive Dec 07 '09 at 18:09
  • @Chris, the breaking changes are the whole reason why it's Python 3 instead of Python 2.8 or something. Overall the changes are very reasonable, and everything is well-documented as noted above. Python 3 cleans up some warts that have been in Python for years, and makes some performance improvements, in ways that *had* to break a few things that used to work. Overall it's worth it. And they have no plans to break things again anytime soon; in fact there is a temporary freeze on new features, in order to give people time to assimilate the new Python 3 stuff. – steveha Dec 07 '09 at 18:13
  • @steveha: I get the reasoning for it. I was just commenting on the status of the internet and the fact that most of the python examples out there use code written for the 2.x codebase. I'll just have to read up on the "What's New" info and go from there, no biggie. – Chris Dec 07 '09 at 18:16
  • *"status of the internet"*. Now That's amusing! not much we can do.. we should talk more about Py 3 on stackoverflow. Read the Official tutorial on Python, it's up to date: http://docs.python.org/3.1/tutorial/ – u0b34a0f6ae Dec 07 '09 at 19:16
4

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)!
miku
  • 181,842
  • 47
  • 306
  • 310
3

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

print (recip)
eduffy
  • 39,140
  • 13
  • 95
  • 92
-1

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

print("write your code")

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 04 '22 at 18:19