118

I'm learning Python and can't even write the first example:

print 2 ** 100

this gives SyntaxError: invalid syntax

pointing at the 2.

Why is this? I'm using version 3.1

4
  • 1
    Where did you find this example? Is it in a book or a website? Commented Jun 2, 2009 at 1:20
  • It might be Learning Python. Commented Jun 2, 2009 at 2:27
  • jleedev is correct; it is OReilly Learning Python 3rd edition 2007. Commented Jun 5, 2009 at 8:34
  • Can also happen in Python 2.x if from __future__ import print_function is used. Then, the print functions behave as in Pyhton 3. Commented Aug 24, 2019 at 16:16

4 Answers 4

245

That is because in Python 3, they have replaced the print statement with the print function.

The syntax is now more or less the same as before, but it requires parens:

From the "what's new in python 3" docs:

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

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)!
Sign up to request clarification or add additional context in comments.

1 Comment

14

You need parentheses:

print(2**100)

Comments

8

They changed print in Python 3. In 2 it was a statement, now it is a function and requires parenthesis.

Here's the docs from Python 3.0.

Comments

2

The syntax is changed in new 3.x releases rather than old 2.x releases: for example in python 2.x you can write: print "Hi new world" but in the new 3.x release you need to use the new syntax and write it like this: print("Hi new world")

check the documentation: http://docs.python.org/3.3/library/functions.html?highlight=print#print

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.