SyntaxError: Missing parentheses in call to 'print' (Python)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# SyntaxError: Missing parentheses in call to 'print' (Python)

The Python "SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?" occurs when we forget to call print() as a function.

To solve the error, call the print function with parentheses, e.g. print('hello world').

syntaxerror missing parenthesis in call to print

Here is an example of how the error occurs.

main.py
name = 'Bobby' # ⛔️ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? print 'hello ' + name

missing parentheses in call to print

The code above uses print as a statement, which is the older syntax that was used in Python 2.

main.py
# ⛔️ This code only works in Python 2 print 'bobbyhadz.com' # ✅ This code works in Python 3 print('bobbyhadz.com')

If you need to check your Python version, use the python --version command.

shell
python --version

get python version

# Call print() as a function

From Python 3 onwards, print() is now a function and should be called with parentheses.

main.py
name = 'Bobby' print('hello ' + name) # 👉️ "hello Bobby"

We called the print() function using parentheses, passing it the string we want to print.

Using print as an expression is no longer valid in Python 3.

main.py
# ⛔️ This no longer works in Python 3 print 'bobbyhadz.com' # ✅ Call print() as a function instead print('bobbyhadz.com')

You can also specify a sep (separator) keyword argument if you need to.

main.py
print('a', 'b', 'c', sep="_") # 👉️ "a_b_c" print('a', 'b', 'c', sep="_", end="!!!") # 👉️ "a_b_c!!!"

The string we passed for the end keyword argument is inserted at the end of the string.

# Using the print() function with formatted string literals

The print() function is commonly used with formatted string literals.

main.py
name = 'Bobby' salary = 100 # Employee name: # Bobby # salary: 200 print(f'Employee name: \n {name} \n salary: {salary * 2}')

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

# If your code should run in both Python 2 and Python 3

You can add an import statement to import the print function from the __future__ module if your code should be able to run in both Python 2 and Python 3.

main.py
# ✅ Can now use the print() function in Python 3 from __future__ import print_function print('bobbyhadz.com')

We imported the print_function from __future__, so now we are able to use print as a function in Python 2.

You can use this approach to make your code Python 3 compatible.

# Use the 2to3 package to make your code Python 3 compatible

You can install the 2to3 package by running the following command.

shell
pip install 2to3 pip3 install 2to3 python -m pip install 2to3 python3 -m pip install 2to3 py -m pip install 2to3

The package is used to make your code Python 3 compatible.

You can run the package for a single file with the following command.

shell
2to3 -w main.py

using 2to3 to make code python3 compatible

Make sure to replace main.py with the name of your module.

You can also use the 2to3 module on all files in the directory.

main.py
2to3 -w .

The print statements in your code should get converted to calls to the print() function as shown in the gif.

Want to learn more about using the print() function in Python? Check out these resources: How to Print on the Same Line in Python,How to print Integer values in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.