What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
Asked Answered
G

11

461

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?


See Getting SyntaxError for print with keyword argument end=' ' for the opposite problem.

See Python 3 print without parenthesis for workarounds, and confirmation that print cannot be made to work like a statement in Python 3.

Goldy answered 22/8, 2014 at 10:58 Comment(2)
Note as a special case: the error message may be different depending on the exact Python version (more recent versions offer an explicit suggestion Did you mean print("Hello, World!")?) and exactly what should be printed (for example, in 3.8, print int() simply gives SyntaxError: invalid syntax).Ole
(Apparently, very old versions only said SyntaxError: invalid syntax even when given a string literal.)Ole
G
685

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.

Goldy answered 22/8, 2014 at 10:59 Comment(7)
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 :)Goldy
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)Goldy
Hello! I think this tool can help someone docs.python.org/2/library/2to3.htmlIgal
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+Barsky
How do you force the system to use Python 2.7 vs 3? Perhaps this is a good place for it.Drusilla
This is a real PITA when you have both 27 ad 37 running on the same box...Dismast
@Drusilla You can use update-alternatives to set your preferred version of python: sudo update-alternatives --config python and set the default version.Probably
P
37

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

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpaVcxKnhrWR3QWRft/comics/python.png

Since Python 3.0 you have to write:

print("Hello, World!")

And someone has still to write that antigravity library :(

Piscatelli answered 24/12, 2017 at 13:56 Comment(2)
antigravity is there though ... have you tried importing it? ;)Exclaim
AntiGravity Easter EggErysipelas
R
26

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.

Rhetorician answered 31/7, 2017 at 6:46 Comment(3)
Only those who want to destroy a language will change the syntax to that extent. What was natural to do was to retain both syntax as valid.Upcountry
@Upcountry not at all. It would make absolutely no sense to have one function, in the entire language, that can also be called with an alternate syntax, based on its name. Incidentally, your comment came almost twelve years after the first 3.x release, and after all support (even security fixes) for 2.x had been completely dropped by the dev team (this is after they made a special exception in their usual release schedule to accomodate migration), and at a time when an overwhelming majority of Python developers were already happily using 3.x.Ole
So much for backwards compability in a programming language. In major use.Ceaseless
Z
15

In Python 3.x, print is now a function, rather than a statement as it was in 2.x.

Therefore, print is used by calling it as a function, and thus parentheses are needed:

Python 2.x: print "Lord of the Rings"

Python 3.x: print("Lord of the Rings")

There are multiple advantages to making print a function:

  • The print function offers more flexibility when printing multiple values (which are now arguments to the function). In particular, it allows for argument splatting:
>>> items = ['foo', 'bar', 'baz']
>>> print(*items, sep='+')
foo+bar+baz
  • The behavior of calls to print can be replaced by simply writing a new function named print. This would be impossible with the print statement.
Zaccaria answered 22/9, 2019 at 20:1 Comment(1)
Also, the print function can be used as a higher-order function, i.e., passed to other functions. This rarely comes up, however.Ole
C
8

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=', ')
Cistaceous answered 29/3, 2018 at 8:31 Comment(1)
Didn't work for me in Python3 even with that import. Code is without parentheses.Shambles
E
6

In Python 3, you can only print as:

print("STRING")

But in Python 2, the parentheses are not necessary.

Elwoodelwyn answered 26/5, 2017 at 0:9 Comment(0)
P
5

I could also just add that I knew everything about the syntax change between Python2.7 and Python3, and my code was correctly written as print("string") and even print(f"string")...

But after some time of debugging I realized that my bash script was calling python like:

python file_name.py

which had the effect of calling my python script by default using python2.7 which gave the error. So I changed my bash script to:

python3 file_name.py

which of coarse uses python3 to run the script which fixed the error.

Pich answered 2/1, 2020 at 13:56 Comment(1)
You could also add a shebang to the top of the file to state which Python to load the file withAlainaalaine
R
3

print('Hello, World!')

You're using python 3, where you need brackets when printing.

Recite answered 19/4, 2021 at 18:43 Comment(0)
R
3

So I was getting this error

from trp import BoundingBox, Document
File "C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp\__init__.py", line 31
print ip
      ^ 
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(ip)?

This is a Python package error, in which Python2 has been used and you are probably running this on Python3.

One solution could be to convert Python2 print something to Python3 print(something) for every line in each file in the package folder, which is not a good idea😅. I mean, you can do it but still there are better ways.

To perform the same task, there is a package named 2to3 in Python which converts Python2 scripts to Python3 scripts. To install it, execute the 👇 command in terminal..

pip install 2to3

Then change the directory in terminal to the location where the package files are present, in my case - C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp

Now execute the command 👇

2to3 . -w

and voila, all the Python2 files in that directory will be converted to Python3.

Note:- The above commands hold true for other operating systems as well. Only Python package path will vary as per the system.

Repay answered 22/11, 2021 at 8:57 Comment(0)
P
2

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).

Plotinus answered 5/6, 2018 at 16:39 Comment(0)
L
1

print "text" is not the way of printing text in python as this won't work print("text") will print said text on your screen in the command line

Lovesome answered 15/3, 2022 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.