In Python 3, you must enclose all print statements with parenthesis. If you try to print out a string to the console without enclosing the string in parenthesis, you’ll encounter the “SyntaxError: Missing parentheses in call to ‘print’” error.
This guide discusses what this error means and how to use the print statement in Python. We’ll walk through an example of this error so you can learn how to solve it.
SyntaxError: Missing parentheses in call to ‘print’
Python 3 is the third major update to the programming language. In recent years, it has become the preferred version of Python to use.
Python 3 changed the way that print statements are written. The standalone
In Python 3,
An Example Scenario
Write a program that prints out the names of all the students in a fourth grade class whose names begin with “A”. To start, define a list that contains the names of the students in the class:
students = ["Alex", "Alexander", "Piper", "Molly", "Hannah"]
Next, write a for loop that iterates over all the items in this list. In the for loop, we’ll use an
if statement to check if each name begins with “A”:
for s in students: if s.startswith("A") == True: print s
The startswith() method checks if a string starts with a particular character or set of characters. The code checks whether each name in the “students” list begins with “A”.
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Add an extra print statement to the end of the code that tells us the program has finished running:
print "Above are all the students whose names begin with A."
Now you’re ready to run the program:
File "main.py", line 5 print s ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(s)?
The code informs us that there is a syntax error in the program.
Handily, Python already offers the solution to the problem in the error message.
This is because, in previous versions of Python 3, forgetting to include parenthesis around a print statement raised an error which only showed “invalid syntax”. This message is ambiguous because invalid syntax can be caused by a number of issues. Thus, Python introduced the new “missing parenthesis” error message primary to help users.
To solve this problem, surround all of the values you want to print to the console in parenthesis:
for s in students: if s.startswith("A") == True: print(s) print("Above are all the students whose names begin with A.")
You have enclosed the “s” on the
Alex Alexander Above are all the students whose names begin with A.
Our code shows us that there are two students whose names begin with an A. Once our list of students has been iterated over, our program prints out a message that describes the output.
The Python “SyntaxError: Missing parentheses in call to ‘print’” error is raised when you try to print a value to the console without enclosing that value in parenthesis.
To solve this error, add parentheses around any statements you want to print to the console. This is because, in Python 3,
Now you have the knowledge you need to fix this common Python error like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.