By ncoghlan


2014-08-22 10:58:51 8 Comments

When I try to use a print statement in Python, it gives me this error:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: Missing parentheses in call to 'print'

What does that mean?

6 comments

@Sagar balai 2017-07-31 06:46:51

There is a change in syntax from Python 2 to Python 3. In Python 2,

print "Hello, World!" 

will work but in Python 3, use parentheses as

print("Hello, World!")

This is equivalent syntax to Scala and near to Java.

@Chad Van De Hey 2018-06-05 16:39:36

Outside of the direct answers here, one should note the other key difference between python 2 and 3. The official python wiki goes into almost all of the major differences and focuses on when you should use either of the versions. This blog post also does a fine job of explaining the current python universe and the somehow unsolved puzzle of moving to python 3.

As far as I can tell, you are beginning to learn the python language. You should consider the aforementioned articles before you continue down the python 3 route. Not only will you have to change some of your syntax, you will also need to think about which packages will be available to you (an advantage of python 2) and potential optimizations that could be made in your code (an advantage of python 3).

@Christian 2017-12-24 13:56:37

Unfortunately, the old 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 has still to write that antigravity library :(

@tyrion 2018-02-18 09:58:11

antigravity is there though ... have you tried importing it? ;)

@jpaugh 2018-05-07 21:56:39

@Larry 2017-05-26 00:09:30

In Python 3, you can only print as:

print("STRING")

But in Python 2, the parentheses are not necessary.

@ncoghlan 2014-08-22 10:58:51

This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement:

print "Hello, World!"

The statement above does not work in Python 3. In Python 3 you need to add parentheses around the value to be printed:

print("Hello, World!")

“SyntaxError: Missing parentheses in call to 'print'” is a new error message that was added in Python 3.4.2 primarily to help users that are trying to follow a Python 2 tutorial while running Python 3.

In Python 3, printing values changed from being a distinct statement to being an ordinary function call, so it now needs parentheses:

>>> print("Hello, World!")
Hello, World!

In earlier versions of Python 3, the interpreter just reports a generic syntax error, without providing any useful hints as to what might be going wrong:

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

As for why print became an ordinary function in Python 3, that didn't relate to the basic form of the statement, but rather to how you did more complicated things like printing multiple items to stderr with a trailing space rather than ending the line.

In Python 2:

>>> import sys
>>> print >> sys.stderr, 1, 2, 3,; print >> sys.stderr, 4, 5, 6
1 2 3 4 5 6

In Python 3:

>>> import sys
>>> print(1, 2, 3, file=sys.stderr, end=" "); print(4, 5, 6, file=sys.stderr)
1 2 3 4 5 6

Starting with the Python 3.6.3 release in September 2017, some error messages related to the Python 2.x print syntax have been updated to recommend their Python 3.x counterparts:

>>> print "Hello!"
  File "<stdin>", line 1
    print "Hello!"
                 ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello!")?

Since the "Missing parentheses in call to print" case is a compile time syntax error and hence has access to the raw source code, it's able to include the full text on the rest of the line in the suggested replacement. However, it doesn't currently try to work out the appropriate quotes to place around that expression (that's not impossible, just sufficiently complicated that it hasn't been done).

The TypeError raised for the right shift operator has also been customised:

>>> print >> sys.stderr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?

Since this error is raised when the code runs, rather than when it is compiled, it doesn't have access to the raw source code, and hence uses meta-variables (<message> and <output_stream>) in the suggested replacement expression instead of whatever the user actually typed. Unlike the syntax error case, it's straightforward to place quotes around the Python expression in the custom right shift error message.

@ncoghlan 2015-05-11 04:33:28

My thanks to @antti-haapala for adding the summary at the top that directly answers the question before continuing on to the longer explanation of the origins of the error message :)

@ncoghlan 2015-05-13 02:13:07

I also switched the answer to community wiki, as steadily accumulating further SO rep for this doesn't feel right to me (see bugs.python.org/issue21669 for the background on how the error message and this SO question co-evolved)

@Jhonatas Kleinkauff 2016-06-07 22:58:31

Hello! I think this tool can help someone docs.python.org/2/library/2to3.html

@Ralf 2016-10-24 11:20:19

Add the line from future import print_function in your 2.7 file to add new python 3 print() lines to your code. Hence the code becomes compatible to 2.7+ and 3.0+

@Praxiteles 2017-11-19 06:56:19

How do you force the system to use Python 2.7 vs 3? Perhaps this is a good place for it.

@Lukasz 2018-03-29 08:31:53

If your code should work in both Python 2 and 3, you can achieve this by loading this at the beginning of your program:

from __future__ import print_function   # If code has to work in Python 2 and 3!

Then you can print in the Python 3 way:

print("python")

If you want to print something without creating a new line - you can do this:

for number in range(0, 10):
    print(number, end=', ')

Related Questions

Sponsored Content

18 Answered Questions

[SOLVED] Convert bytes to a string?

11 Answered Questions

[SOLVED] Converting integer to string?

12 Answered Questions

[SOLVED] Meaning of @classmethod and @staticmethod for beginner?

9 Answered Questions

[SOLVED] Proper way to declare custom exceptions in modern Python?

38 Answered Questions

[SOLVED] What does the "yield" keyword do?

44 Answered Questions

[SOLVED] Replacements for switch statement in Python?

9 Answered Questions

14 Answered Questions

[SOLVED] What is the meaning of a single and a double underscore before an object name?

5 Answered Questions

[SOLVED] What does the star operator mean?

191 Answered Questions

[SOLVED] Hidden features of Python

Sponsored Content