0

I am a python newbie. I am trying to understand why I get this error:

File "python-challenge-1.py", line 12
    print original.translate(table)
             ^

SyntaxError: invalid syntax

Here is the full code:

import string

original = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc " \
    "dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq " \
    "rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu " \
    "ynnjw ml rfc spj."

table = string.maketrans(
"abcdefghijklmnopqrstuvwxyz", "cdefghijklmnopqrstuvwxyzab"
)

print original.translate(table)
Tkingovr
  • 1,390
  • 16
  • 33

2 Answers2

5

print() is a function in Python 3, but print is a keyword in Python 2. Check PEP 3105 for more info. Also here's a good question on SO.

You should try changing it to this:

print(original.translate(table))
Community
  • 1
  • 1
Josh Rumbut
  • 2,640
  • 2
  • 32
  • 43
1

as @JoshRumbut has pointed out, print() is really a function.

In Python2, you could write print 12, but this has been abandoned in Python3, so you now must write print(12).

Luckily, Python2 understands the functional version as well, so in general you are better off using this one.

umläute
  • 28,885
  • 9
  • 68
  • 122
  • 1
    Not to be overly pedantic but technically Python2's version is a statement, print('foo') works in Python 2 because the expression following the print statement is coerced to a string value and then passed to print. This means print ( is. Print (1,2) means "print the tuple (1,2)" whereas "print (1)" means "print the value of the expression "(1)" which is equal to just "1". The output of print(1,2) then on Python2, while it will run, will be different to that of Python3. – W.Prins Nov 26 '15 at 09:54
  • 1
    @W.Prins: What you say is certainly correct, however, later versions of Python 2 _do_ support the `print` function. It is normally masked by the `print` statement, but it can be exposed by putting `from __future__ import print_function` before any other `import` statements. (FWIW, I use Python 2.6.6). – PM 2Ring Nov 26 '15 at 13:27
  • @PM2Ring Yes indeed. Thanks for adding that as well. – W.Prins Nov 27 '15 at 14:18