Home >Backend Development >Python Tutorial >Why Am I Getting a 'SyntaxError: Missing Parentheses in Call to 'print'' in Python 3?

Why Am I Getting a 'SyntaxError: Missing Parentheses in Call to 'print'' in Python 3?

Barbara Streisand
Barbara StreisandOriginal
2024-12-21 01:25:10257browse

Why Am I Getting a

SyntaxError: Missing Parentheses in Call to 'print'

When attempting to use the print statement in Python 3 without parentheses, you encounter the error "SyntaxError: Missing parentheses in call to 'print'".

Cause:

In Python 3, the print statement has been replaced with a print() function that requires parentheses around the value to be printed.

Example of Incorrect Usage (Python 2 Syntax):

print "Hello, World!"

Solution:

Enclose the value to be printed within parentheses in Python 3.

print("Hello, World!")

History:

In earlier versions of Python 3, the interpreter reported a generic syntax error without specific hints. However, starting with Python 3.6.3, the error message has been updated to provide a suggested fix:

SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello!")?

Additional Notes:

The print() function in Python provides more control over output formatting compared to Python 2's print statement. For example, to print multiple items to stderr with a trailing space, use the file=sys.stderr and end=" " arguments.

import sys
print(1, 2, 3, file=sys.stderr, end=" ")
print(4, 5, 6, file=sys.stderr)

The above is the detailed content of Why Am I Getting a 'SyntaxError: Missing Parentheses in Call to 'print'' in Python 3?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn