16

After installing python 3.1, I cannot print anything. Here is an example:

>>> print "Hello World"
  File "<stdin>", line 1
    print "Hello World"
                      ^
SyntaxError: invalid syntax
>>>

How do I fix this error?

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
asd32
  • 191
  • 1
  • 1
  • 6

5 Answers5

18

Try this:

>>> print "Hello World!"
  File "<stdin>", line 1
    print "Hello World!"
SyntaxError: invalid syntax
>>> print("Hello World!")
Hello World!

Python 3.X changed how print works, and now requires parentheses around the arguments. Check out the python docs for more.

John Howard
  • 61,037
  • 23
  • 50
  • 66
  • 1
    Of course the alternative is to install python 2.x - there are many more tutorials out there for the 2.x releases. – zdav Jul 30 '10 at 00:21
  • 3
    The real alternative was *NOT* break basic functionality. – jww Mar 15 '14 at 15:39
4

if something's going wrong, you can always try to call for help:

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.

and there you might see, tha the syntax of that print thing is print(something)

funny is, that in python 2, you get just an error message:

>>> help(print)
SyntaxError: invalid syntax

it's because in python < 3, print function was not a function, but a keyword (just like e.g. for or or)

mykhal
  • 19,175
  • 11
  • 72
  • 80
3

This threw me off too!

print("Hello World")

The changes were documented here: http://docs.python.org/release/3.0.1/whatsnew/3.0.html

sholsapp
  • 15,542
  • 10
  • 50
  • 67
3

If you are learning Python from a textbook that is telling you to type print "Hello World", I recommend installing the Python version mentioned in the textbook.

Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
2

Yeah strange as it may seem i spent an hour trying to figure it out . At first could not believe how dumb i was to not even get the syntax right . This seems a consolation that python has changed .

print ("Hello World") 

seems the way to go from now !

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
rockstar
  • 3,512
  • 6
  • 40
  • 63