✋ Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza
The error “SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(…)?” in Python happens when you use an old-style print statement (e.g., print 'some value'
) in Python 3.
This long error looks like this:
File /dwd/sandbox/test.py, line 1
print 'some text here'
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
As the error explains, from Python 3, you must call the print()
function:
# 🚫 SyntaxError
print 'some text here'
# ✅ Correct
print('some text here')
You might get this error if you're running an old code by your Python 3 interpreter or copying a code snippet from an old post on Stackoverflow.
How to fix the "SyntaxError: Missing parentheses in call to 'print'"?
All you need to do is to call print()
with your string literal(s) as its argument(s) - and do the same to every old-style print statement in your code.
The print()
function is much more robust than its predecessor. Python 3 enables you to adjust the print()
function's behavior based on your requirements.
For instance, to print a list of text values separated by a character (e.g., a space or comma), you can pass them as multiple arguments to the print()
function:
# ✅ Using print() with multiple arguments:
price = 49.99
print('The value is', price)
# output: The value is 49.99
As you can see, the arguments are separated by whitespace. To change the delimiter, you can use the sep
keyword argument:
print('banana', 'apple', 'orange', sep=', ')
# output: banana, apple, orange
Python 3 takes the dynamic text generation to a new level by providing formatted string literals (a.k.a f-strings) and the print()
function.
One of the benefits of f-strings is concatenating values of different types (e.g., integers and strings) without having to cast them to string value. You create an f-string by prefixing it with f
or F
and writing expressions inside curly braces ({}
):
user = {
'name': 'John',
'score': 75
}
print(f'User: {user[name]}, Score: {user[score]}')
# output: User: John, Score: 75
In python 2.7, you'd have to use the +
operator or printf-style formatting.
Please note that f-string was added to Python from version 3.6. For older versions, check out the str.format()
function.
And that's how you fix the "SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?" error in Python 3.
Alright, I think it does it. I hope this quick guide helped you solve your problem.
Thanks for reading.
Top comments (0)