13

My code:

print "Hello World!"

I even tried adding a semicolon behind, but everytime I save and run (as Python run) it says:

File "E:\Software\Eclipse\Workspace\Python1\src\main.py", line 1 print "Hello World!";

SyntaxError: invalid syntax

I have no idea why.

  • Thanks for this thread! It's a so easy but still annoying problem when it dosen't work with a simple print! I used 3.xx and that's why print 'Hello World!' didn't work :P So all 3.xx use print("Hello World!") :D –  Jun 10 '11 at 13:33

3 Answers3

35

What version of Python are you using? Python 2.X has print as a keyword, but Python 3.X only has print() as a function - you'd need to use print("Hello, World!") instead.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Ah, I see. Problem solved. Just curious, is it a must to have a semi-colon at the end of every line as in the case of PHP/Javascript? Hm, maybe I should have used the latest 2.X instead of 3.X, I thought 3.X would be better since it's newer. –  Dec 18 '09 at 02:15
  • Python does not require semicolons (and in fact discourages them). – Amber Dec 18 '09 at 02:19
  • For a more comprehensive list of differences, take a look at http://docs.python.org/dev/3.0/whatsnew/3.0.html – Amber Dec 18 '09 at 02:20
4

This is kind of a longshot but - if you're running python 3.0 that is invalid syntax. Try

print("Hello World!") 

to see if this is the case.

Mongoose
  • 4,555
  • 1
  • 16
  • 7
1

In Python, indentation is really important... Have you check your indentation? Also, lose the ; (don't need it).

correct:

print("hello") or print "hello" (for < 3.0)

not correct:

...print("hello") or print "hello" (for < 3.0)

where . denotes spaces.

jldupont
  • 93,734
  • 56
  • 203
  • 318