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 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. 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). 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

188
print("Hello, World!")

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

Sign up to request clarification or add additional context in comments.

7 Comments

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.
"requires parenthesis" is not really the adequate explanation as to the change from 2.x to 3 :)
@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.
@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.
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.
|
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 Comments

Whoa... your xkcd reference even has the hovertext. Is that xkcd's doing or yours?
@inetknght: If you look at the markup behind the post you see how the hovertext is done.
@inetknght the title attribute in <img> tag has been used for that purpose.
@KNU : Markdown has no <img> tag. The formatting works differently.
16

In python 3.x. you use

print("Hello, World")

In Python 2.x. you use

print "Hello, World!"

2 Comments

Using 3.x requires less enthusiasm?
`` @LShaver Yes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.