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?

7
  • 123
    I suspect this will become the most-frequently-asked Python question for the next couple of years. Commented Jul 3, 2009 at 0:57
  • 4
    Please post the results of python --version
    – S.Lott
    Commented Jul 3, 2009 at 1: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
    Commented Jul 3, 2009 at 1:59
  • 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
    Commented Dec 13, 2011 at 21:07
  • 1
    @GregHewgill viewed 53k times, you weren't too wrong. Commented Jan 21, 2014 at 2:48

3 Answers 3

186
print("Hello, World!")

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

7
  • 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
    Commented Jul 3, 2009 at 0:29
  • 1
    "requires parenthesis" is not really the adequate explanation as to the change from 2.x to 3 :) Commented Jul 3, 2009 at 0: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. Commented Jul 3, 2009 at 0: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
    Commented Jul 3, 2009 at 0: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
    Commented Jul 3, 2009 at 1:06
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 :(

5
  • 39
    svn.python.org/view/python/trunk/Lib/…
    – Blixt
    Commented Jul 14, 2009 at 11:58
  • 3
    Whoa... your xkcd reference even has the hovertext. Is that xkcd's doing or yours?
    – inetknght
    Commented Feb 27, 2014 at 18:42
  • 3
    @inetknght: If you look at the markup behind the post you see how the hovertext is done.
    – Christian
    Commented Feb 27, 2014 at 19:09
  • @inetknght the title attribute in <img> tag has been used for that purpose.
    – KNU
    Commented Nov 14, 2014 at 12:39
  • @KNU : Markdown has no <img> tag. The formatting works differently.
    – Christian
    Commented Nov 14, 2014 at 15:02
16

In python 3.x. you use

print("Hello, World")

In Python 2.x. you use

print "Hello, World!"
2
  • 16
    Using 3.x requires less enthusiasm?
    – LShaver
    Commented Mar 5, 2017 at 18:31
  • 1
    `` @LShaver Yes.
    – Jake Duth
    Commented May 22, 2018 at 2:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.