How do you fix "SyntaxError: Missing parentheses in call to 'print'" in Python 3? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you fix ""SyntaxError: Missing parentheses in call to 'print'"" in Python 3?

message-square
0

Wrap the arguments of every print statement in parentheses—changing Python 2’s print "Hello" syntax to Python 3’s function call print("Hello")—because print became a built-in function in Python 3.0+, or automatically convert entire legacy Python 2 files in bulk using 2to3 (Python $\le$ 3.12) or ruff check --select UP --fix / lib2to3-compatible modernizers when migrating larger codebases.

Basic Example

# Legacy Python 2 syntax (triggers `SyntaxError: Missing parentheses in call to 'print'` in Python 3):
# print "Hello, world!"
# print "Total:", 42

# 1. Recommended: Call `print()` as a function with parentheses in Python 3
print("Hello, world!")
# Output: Hello, world!

print("Total:", 42)
# Output: Total: 42

# 2. Replacing Python 2 trailing comma (suppressing newline) with `end=" "`:
# Python 2: print "Loading...",
print("Loading...", end=" ")
print("Done!")
# Output: Loading... Done!

# 3. Replacing Python 2 chevron stream redirection (`print >> sys.stderr, ...`) with `file=`:
import sys
# Python 2: print >> sys.stderr, "Error occurred"
print("Error occurred", file=sys.stderr)

Key Details & Edge Cases

  • Tuple trap when wrapping existing parenthesized expressions (print("a", "b") vs. print(("a", "b"))):
    • In Python 2, print ("a", "b") printed a tuple ('a', 'b') because print was a keyword followed by a tuple literal. In Python 3, print("a", "b") passes two separate arguments and prints a b separated by spaces. If you actually want to print a tuple object, double the parentheses: print(("a", "b")).
  • Migrating legacy Python 2 print modifiers (end=, sep=, file=):
    • Trailing comma (print x,): Replaced by print(x, end=" ") (or end="" when no trailing space is desired).
    • Stream redirection (print >> f, x): Replaced by print(x, file=f).
    • Empty blank line (print): Replaced by print().
  • Third-party package installed for Python 2 (pip install pulling old source code):
    • If SyntaxError: Missing parentheses in call to 'print' occurs inside site-packages/ during pip install <pkg> or import <pkg>, you are installing an abandoned Python 2-only library (or an ancient version pin in requirements.txt). Upgrade to a Python 3-compatible release (python -m pip install --upgrade <pkg>) or replace the unmaintained package.
  • Automated bulk conversion across multi-file Python 2 codebases:
    • On Python $\le$ 3.12, run python -m lib2to3 -f print -w src/ to rewrite only print statements in-place across an entire directory without altering other code. (Note: lib2to3 was removed from the standard library in Python 3.13; on Python 3.13+, use pyupgrade or 2to3 from PyPI).
  • Writing dual-compatible Python 2.7 + Python 3 code (from __future__ import print_function):
    • If a legacy script must still run under Python 2.7 alongside Python 3, place from __future__ import print_function as the very first non-comment line of the file so Python 2 enforces Python 3’s print(...) function syntax too.

Comparison of Python 2 print Statement vs. Python 3 print() Function Syntax

Legacy Python 2 Statement Modern Python 3 print() Equivalent Printed Output Best Used When
print "Hello" print("Hello") Hello\n Printing standard strings or objects with a trailing newline.
print "A", "B" print("A", "B") (or sep=", ") A B\n Printing multiple space-separated (or custom sep=) values.
print "Working...", print("Working...", end=" ") Working... (no newline) Suppressing the default \n newline (e.g., inline progress updates).
print >> sys.stderr, "Err" print("Err", file=sys.stderr) Err\n (sent to stderr) Writing warnings or error diagnostics to sys.stderr or an open file handle.
print print() \n (blank line) Outputting an empty line separator.