Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print syntax error with python 3 [duplicate]

After installing python 3.1, I cannot print anything. Here is an example:

>>> print "Hello World"
  File "<stdin>", line 1
    print "Hello World"
                      ^
SyntaxError: invalid syntax
>>>

How do I fix this error?

like image 610
asd32 Avatar asked Jul 30 '10 00:07

asd32


People also ask

How do I fix the print syntax error in Python?

The Python “SyntaxError: Missing parentheses in call to 'print'” error is raised when you try to print a value to the console without enclosing that value in parenthesis. To solve this error, add parentheses around any statements you want to print to the console. This is because, in Python 3, print is not a statement.

What is print in python2?

Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.


2 Answers

Try this:

>>> print "Hello World!"
  File "<stdin>", line 1
    print "Hello World!"
SyntaxError: invalid syntax
>>> print("Hello World!")
Hello World!

Python 3.X changed how print works, and now requires parentheses around the arguments. Check out the python docs for more.

like image 190
John Howard Avatar answered Oct 03 '22 19:10

John Howard


if something's going wrong, you can always try to call for help:

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.

and there you might see, tha the syntax of that print thing is print(something)

funny is, that in python 2, you get just an error message:

>>> help(print)
SyntaxError: invalid syntax

it's because in python < 3, print function was not a function, but a keyword (just like e.g. for or or)

like image 43
mykhal Avatar answered Oct 03 '22 21:10

mykhal