136

I tried running a python script:

print "Hello, World!" 

And I get this error:

  File "hello.py", line 1
    print "Hello, World!"
                        ^
SyntaxError: invalid syntax

What is going on?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
MiffTheFox
  • 21,302
  • 14
  • 69
  • 94
  • 123
    I suspect this will become the most-frequently-asked Python question for the next couple of years. – Greg Hewgill Jul 03 '09 at 00:57
  • 4
    Please post the results of `python --version` – S.Lott Jul 03 '09 at 01:06
  • 8
    Just a thought: Python 3.0 should come with "Py3k warnings" on by default. Think of how many thousands of SO/newsgroup questions could be prevented by doing this. – RexE Jul 03 '09 at 01:59
  • Yes, something as simple as the warning on ubuntu linux - which pops up when you type a command whose executable isn't installed - telling you how to install it and what command to use. – viksit Jul 16 '09 at 20:21
  • 18
    First time I see somebody asking a question about how to implement "Hello World" in a language. Makes you wonder what that says about the language if that's causing people trouble already... (since it's usually given as the first code example in any introduction). – Peladao Dec 13 '11 at 21:07
  • Why can't it be simple as that? - print "Ukinayo" - Makes the world perfectly round. – Jayson Ragasa Jul 18 '13 at 20:11
  • 1
    @GregHewgill viewed 53k times, you weren't too wrong. – TankorSmash Jan 21 '14 at 02:48

3 Answers3

186
print("Hello, World!")

You are probably using Python 3.0, where print is now a function (hence the parenthesis) instead of a statement.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
Unknown
  • 45,913
  • 27
  • 138
  • 182
  • 1
    Thank you, this worked. I don't know why this isn't more common knowledge, because I just copy-pasted from the first Google result for Python Hello World. – MiffTheFox Jul 03 '09 at 00:29
  • 1
    "requires parenthesis" is not really the adequate explanation as to the change from 2.x to 3 :) – Paolo Bergantino Jul 03 '09 at 00:29
  • 15
    @MiffTheFox: Python 2.x uses print as a statement. The relatively new Python 3 made print a function instead. The majority of Python programmers are still using 2.x because of its extensive library and framework support, so 3.0 isn't nearly as adopted as you'd expect for now. – Paolo Bergantino Jul 03 '09 at 00:31
  • 2
    @paulo, its the most succinct. If I had said, it is now a function, I would have to then explain what the difference between a statement and an expression is and how a function fits into the whole picture. – Unknown Jul 03 '09 at 00:34
  • 3
    They should have a special error message for cases like this with a bit more explanation. With all the documentation out there for Python 2, this kind of incompatible syntax change is bound to frustrate the uninitiated a lot. – Thilo Jul 03 '09 at 01:06
  • Well, this certainly seems like a step in the wrong direction! – Andrew Johnson Jul 03 '09 at 01:43
  • @Andrew: in what sense is this a step in the wrong direction? Now that print is a function, it can be treated like all other functions (and can thus be passed around, etc.) – Stephan202 Jul 14 '09 at 14:35
115

Unfortunately the xkcd comic isn't completely up to date anymore.

https://imgs.xkcd.com/comics/python.png

Since Python 3.0 you have to write:

print("Hello world!")

And someone still has to write that antigravity library :(

Community
  • 1
  • 1
Christian
  • 25,249
  • 40
  • 134
  • 225
16

In python 3.x. you use

print("Hello, World")

In Python 2.x. you use

print "Hello, World!"
Anish Gupta
  • 2,218
  • 2
  • 23
  • 37