Last updated: Apr 8, 2024
Reading time·3 min
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')
.
Here is an example of how the error occurs.
name = 'Bobby' # ⛔️ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? print 'hello ' + name
The code above uses print
as a statement, which is the older syntax that was
used in Python 2.
# ⛔️ 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.
python --version
From Python 3 onwards, print()
is now a function and should be called with
parentheses.
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.
# ⛔️ 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.
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.
print()
function with formatted string literalsThe print()
function is commonly used with
formatted string literals.
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
.
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}
.
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.
# ✅ 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.
You can install the 2to3 package by running the following command.
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.
2to3 -w main.py
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.
2to3 -w .
The print
statements in your code should get converted to calls to the
print()
function as shown in the gif
.
You can learn more about the related topics by checking out the following tutorials: