Hello World in Python

PythonPython 3.x

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

print("Hello, World!")

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

Solution 2 - Python

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

![https://imgs.xkcd.com/comics/python.png](https://imgs.xkcd.com/comics/python.png "I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I'm leaving you.")

Since Python 3.0 you have to write:

print("Hello world!")

And someone still has to write that antigravity library :(

Solution 3 - Python

In python 3.x. you use

print("Hello, World")

In Python 2.x. you use

print "Hello, World!"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMiffTheFoxView Question on Stackoverflow
Solution 1 - PythonUnknownView Answer on Stackoverflow
Solution 2 - PythonChristianView Answer on Stackoverflow
Solution 3 - PythonAnish GuptaView Answer on Stackoverflow