Borislav Hadzhiev
Sat Apr 30 2022·1 min read
Photo by Jeffrey Lee
The Python "SyntaxError: Missing parentheses in call to 'print'. Did you mean
print(...)?" occurs when we forget to call print()
as a function. To solve the
error, call the print function with parenthesis, e.g. print('hello world')
.
Here is an example of how the error occurs.
name = 'Alice' # ⛔️ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? print 'hello ' + name
The code above uses print
as a statement, which is the older syntax that was
used in Python 2.
From Python 3 onwards, print()
is now a function and should be called with
parenthesis.
name = 'Alice' print('hello ' + name) # 👉️ "hello Alice"
We called the print() function using parenthesis and passed it the string we want to print.
You can also specify a sep
(separator) keyword argument if you need to.
print('a', 'b', 'c', sep="_") # 👉️ "a_b_c" print('a', 'b', 'c', sep="_", end="!!!") # 👉️ "a_b_c!!!"
The string we passed for the end
keyword argument is inserted at the end of
the string.
The print()
function is commonly used with formatted string literals.
name = 'Alice' salary = 100 # Employee name: # Alice # salary: 200 print(f'Employee name: \n {name} \n salary: {salary * 2}')
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.