How to Resolve Python "SyntaxError: Missing parentheses in call to 'print'"
The SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
is a very common error, especially for those transitioning from Python 2 to Python 3 or beginners encountering older code examples. This error occurs because print
, which was a statement in Python 2, became a built-in function in Python 3, requiring parentheses for invocation.
This guide explains this fundamental change and shows how to fix the error by adopting the correct Python 3 syntax.
Understanding the Error: print
Statement vs. print()
Function
The way Python handles printing output changed significantly between major versions:
- Python 2:
print
was a special statement. You used it like this:print "Hello"
orprint my_variable
. No parentheses were required around the items to be printed unless you needed specific formatting control not relevant here. - Python 3:
print
was redesigned as a built-in function. Like all functions, it must be called using parentheses()
, enclosing the arguments you want to print:print("Hello")
orprint(my_variable)
.
The SyntaxError: Missing parentheses...
occurs because the Python 3 interpreter encounters the keyword print
followed by something else (like a variable or a string literal) without the expected parentheses ()
that signify a function call.
The Cause: Using Python 2 print
Syntax in Python 3
The error arises when you write code using the old Python 2 statement syntax but run it using a Python 3 interpreter.
# Error Scenario (running with Python 3)
message = "Hello from Python 2 style!"
try:
# ⛔️ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
print message
except SyntaxError as e:
print(f"Caught Error: {e}")
name = "Alice"
try:
# ⛔️ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
print "User:", name
except SyntaxError as e:
print(f"Caught Error: {e}")
Python 3 syntax requires the parentheses for the function call.
The Solution: Call print()
as a Function with Parentheses
The fix is straightforward: add parentheses ()
around the item(s) you want to print, treating print
as a function call.
# ✅ Corrected Code (for Python 3)
message = "Hello from Python 3 style!"
name = "Tom"
# Call print() as a function
print(message)
# Output: Hello from Python 3 style!
# Pass multiple arguments separated by commas inside the parentheses
print("User:", name)
# Output: User: Tom
# Printing results of expressions
print("Length:", len(message))
# Output: Length: 26
print("Calculation:", 5 * 10)
# Output: Calculation: 50
Output:
Hello from Python 3 style!
User: Tom
Length: 26
Calculation: 50
This is the standard and only correct way to print in Python 3 and later versions.
Using the print()
Function in Python 3
Being a function, print()
in Python 3 accepts various arguments:
Separator (sep
) and End (end
) Arguments
sep
: The string inserted between arguments (defaults to a space' '
).end
: The string appended after the last value (defaults to a newline'\n'
).
print("apple", "banana", "cherry")
# Output: apple banana cherry
print("apple", "banana", "cherry", sep=", ")
# Output: apple, banana, cherry
print("Line 1", end=" | ")
print("Line 2")
# Output: Line 1 | Line 2
Output:
apple banana cherry
apple, banana, cherry
Line 1 | Line 2
Using with f-strings
print()
is commonly used with f-strings for easy formatting:
item = "Widget"
price = 19.99
quantity = 3
print(f"Item: {item}, Price: ${price:.2f}, Total: ${price * quantity:.2f}")
# Output: Item: Widget, Price: $19.99, Total: $59.97
Cross-Version Compatibility (__future__
import)
If you are writing code that needs to run on both Python 2.7 and Python 3.x, you can force Python 2 to adopt the Python 3 print()
function behavior by adding this special import at the very top of your Python 2 file:
# Add this line at the absolute beginning of your .py file
from __future__ import print_function
# Now you can use print() function syntax, even in Python 2.7
print("This works like Python 3 print now")
print("Value:", 42)
This allows you to write code using the Python 3 print()
syntax that remains compatible with Python 2.7. However, for new projects targeting only Python 3, this import is unnecessary.
Automated Code Conversion (2to3
tool)
For larger Python 2 codebases, Python provides a utility called 2to3
that can automatically convert much of the Python 2 syntax (including print
statements) to Python 3 syntax.
You can run it from your terminal:
# Convert a single file in place (creates a backup)
2to3 -w your_script.py
# Convert all Python files in the current directory and subdirectories
2to3 -w .
This tool handles the print
statement conversion automatically, adding the necessary parentheses.
Conclusion
The SyntaxError: Missing parentheses in call to 'print'
is a clear indicator of using outdated Python 2 print
statement syntax in a Python 3 environment.
- The solution is fundamental to Python 3: Always use
print()
as a function, enclosing the arguments within parentheses. - If you need cross-version compatibility, use
from __future__ import print_function
. For converting existing Python 2 code, consider using the2to3
tool. Embracing theprint()
function syntax is essential for writing correct and modern Python code.