0

Possible Duplicate:
SyntaxError near “print”?

I am a first time user of python. I just got Python 3.3 installed on my computer, got the PATH updated and attempted to run my first python program that I just copied and pasted from a tutorial into a new file. I get an error that reads:

File "C:\Users\bmahnke\Desktop\python.py", line 23
      print 'string1 is: ', string1

SyntaxError: invalid syntax

I am not sure what the problem is because I got it from a tutorial site and I'm not certain, but almost, that they wouldn't put a file on there that doesn't compile. So I was wondering what I am doing wrong

Here is the python code that I am using:

#! C:\Python33\python.exe

string1 = 'In this class,'
string2 = 'I am learning to program in'
string3 = 'PYTHON.'

print 'string1 is: ', string1
print 'string2 is: ', string2
print 'string3 is: ', string3
print 'Put them altogether and you get:'
print string1, string2, string3
print string1 + string2 + string3

Any help is appreciated, thanks.

Community
  • 1
  • 1
B-M
  • 1,231
  • 1
  • 19
  • 41

4 Answers4

7

In Python 3.3, print() is a function (in Python 2.x, it was a statement). So the correct syntax is now:

print('string1 is: ', string1)

There is a tool called 2to3.py which converts Python 2 to 3.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

The problem is that in Python 3 print is not a statement, but a normal function. Just put ( and ) around the arguments to the print function and you should be fine.

Michael Wild
  • 24,977
  • 3
  • 43
  • 43
0

This code is for Python 2.x. Print statements in Python 3.x are different as per this section from the Python documentation:

Talvalin
  • 7,789
  • 2
  • 30
  • 40
0

In python3, print is a function:

print('string1 is: ', string1)

etc

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124