TypeError: 'str' object is not callable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: 'str' object is not callable in Python #

The Python "TypeError: 'str' object is not callable" occurs when we try to call a string as a function, e.g. by overriding the built-in str() function. To solve the error, make sure you're not overriding str and resolve any clashes between function and variable names.

typeerror str object is not callable

Here is one example of how the error occurs.

main.py
# 👇️ overriding built-in str() str = 'hello world' # ⛔️ TypeError: 'str' object is not callable print(str(10))

We override the built-in str function setting it to a string primitive and try to call it as a function.

To solve the error in this situation, make sure to rename your variable and restart your script.

main.py
# ✅ Not overriding built-in str() anymore my_str = 'hello world' print(str(10))
If you are trying to access the string at a specific index, use square brackets not parentheses, e.g. my_list[0].

Another common cause of the error is simply trying to call a string as a function.

main.py
my_str = 'hello world' # ⛔️ TypeError: 'str' object is not callable print(my_str())

To solve the error, you either have to remove the parentheses or figure out how the variable got assigned a string instead of a function or a class.

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self, name): # 👇️ this attribute hides the method self.name = name # 👇️ same name as class variable def name(self): return self.name emp = Employee('Alice') # ⛔️ TypeError: 'str' object is not callable print(emp.name())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to rename the class method.

main.py
class Employee(): def __init__(self, name): self.name = name def get_name(self): return self.name emp = Employee('Alice') print(emp.get_name()) # 👉️ "Alice"

Once you rename the method, you will be able to call it without any issues.

Make sure you don't have a function and a variable with the same name.

main.py
def example(): return 'hello world' example = 'hi' # ⛔️ TypeError: 'str' object is not callable print(example())

The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

Conclusion #

The Python "TypeError: 'str' object is not callable" occurs when we try to call a string as a function, e.g. by overriding the built-in str() function. To solve the error, make sure you're not overriding str and resolve any clashes between function and variable names.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.