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 2t (Python $\le$ 3.12) or ruff check --select UP --fix / lib2t-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')becauseprintwas a keyword followed by a tuple literal. In Python 3,print("a", "b")passes two separate arguments and printsa bseparated by spaces. If you actually want to print a tuple object, double the parentheses:print(("a", "b")).
- In Python 2,
- Migrating legacy Python 2
printmodifiers (end=,sep=,file=):- Trailing comma (
print x,): Replaced byprint(x, end=" ")(orend=""when no trailing space is desired). - Stream redirection (
print >> f, x): Replaced byprint(x, file=f). - Empty blank line (
print): Replaced byprint().
- Trailing comma (
- Third-party package installed for Python 2 (
pip installpulling old source code):- If
SyntaxError: Missing parentheses in call to 'print'occurs insidesite-packages/duringpip install <pkg>orimport <pkg>, you are installing an abandoned Python 2-only library (or an ancient version pin inrequirements.txt). Upgrade to a Python 3-compatible release (python -m pip install --upgrade <pkg>) or replace the unmaintained package.
- If
- Automated bulk conversion across multi-file Python 2 codebases:
- On Python $\le$ 3.12, run
python -m lib2to3 -f print -w src/to rewrite onlyprintstatements in-place across an entire directory without altering other code. (Note:lib2twas removed from the standard library in Python 3.13; on Python 3.13+, usepyupgradeor2tfrom PyPI).
- On Python $\le$ 3.12, run
- 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_functionas the very first non-comment line of the file so Python 2 enforces Python 3’sprint(...)function syntax too.
- If a legacy script must still run under Python 2.7 alongside Python 3, place
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. |
You must log in or register to comment.
