TypeError: 'str' object is not callable in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# 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

# Overriding the built-in str() function

Here is one example of how the error occurs.

main.py
# ๐Ÿ‘‡๏ธ Overriding the built-in str() str = 'bobbyhadz.com' # โ›”๏ธ TypeError: 'str' object is not callable print(str(10))

overriding built in str function

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, rename your variable and restart your script.

main.py
# โœ… Not overriding the built-in str() anymore my_str = 'bobbyhadz.com' print(str(10))

Now the variable no longer shadows the built-in str() function, so the issue is resolved.

# Use square brackets to access a string at a specific index

If you are trying to access the string at a specific index, use square brackets [], not parentheses ().

main.py
my_str = 'bobbyhadz.com' # โ›”๏ธ TypeError: 'str' object is not callable print(my_str(0))

The code sample causes the error because we used parentheses when trying to access the string at an index.

To solve the error, use square brackets instead.

main.py
my_str = 'bobbyhadz.com' print(my_str[0]) # ๐Ÿ‘‰๏ธ b print(my_str[1]) # ๐Ÿ‘‰๏ธ o print(my_str[2]) # ๐Ÿ‘‰๏ธ b

use square brackets to access string at specific index

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(a_string) - 1.

You should also use square brackets [] when getting a slice of a string.

main.py
my_str = 'bobbyhadz.com' print(my_str[0:3]) # ๐Ÿ‘‰๏ธ bob print(my_str[0:5]) # ๐Ÿ‘‰๏ธ bobby

using square brackets to slice strings

The syntax for string slicing is a_string[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

If the start index is omitted, it is considered to be 0, if the stop index is omitted, the slice goes to the end of the string.

# Common causes of the error

The error occurs for multiple reasons:

  • Trying to access a string with parentheses () instead of square brackets [].
  • Having a function and a variable with the same name.
  • Overriding a built-in function by mistake and setting it to a string.
  • Having a class method and a class property with the same name.
  • Calling a function that returns a string twice.

# Having a function and a variable with the same name

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

main.py
def example(): return 'bobbyhadz.com' example = 'hi' # โ›”๏ธ TypeError: 'str' object is not callable print(example())

having function and variable with same name

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.

main.py
def example(): return 'bobbyhadz.com' my_str = 'hi' print(example()) # ๐Ÿ‘‰๏ธ bobbyhadz.com

We gave the variable a different name, so it no longer shadows the function.

# Trying to call a string literal as a function

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

main.py
my_str = 'bobbyhadz.com' # โ›”๏ธ 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.

main.py
my_str = 'bobbyhadz.com' print(my_str) # ๐Ÿ‘‰๏ธ bobbyhadz.com

Removing the parentheses resolves the issue.

If the variable should point to a function or a method, you should track down where it got set to a string and correct the assignment.

# Having a class method and a class property with the same name

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('Bobby Hadz') # โ›”๏ธ 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, rename the class method.

main.py
class Employee(): def __init__(self, name): self.name = name def get_name(self): return self.name emp = Employee('Bobby Hadz') print(emp.get_name()) # ๐Ÿ‘‰๏ธ "Bobby Hadz"

Now that the names of the method and class property no longer clash, we can call the method without any issues.

# Calling a function that returns a string twice

Here is another example of how the error occurs.

main.py
def get_str(): return 'bobbyhadz.com' # โ›”๏ธ TypeError: 'str' object is not callable get_str()()

Notice that we used two sets of parentheses.

The first set invoked the get_str function. The function returns a string, so the second set of parentheses invoked the string.

To solve the error, remove the second set of parentheses.

main.py
def get_str(): return 'bobbyhadz.com' print(get_str()) # ๐Ÿ‘‰๏ธ bobbyhadz.com

Now we only call the function once and print the result.

# Conclusion

To solve the "TypeError: 'str' object is not callable" error, make sure:

  • You aren't trying to access a string with parentheses () instead of square brackets [].
  • You don't have a function and a variable with the same name.
  • You aren't overriding a built-in function and setting it to a string.
  • You don't have a class method and a property with the same name.
  • You aren't calling a function that returns a string twice.
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.

Copyright ยฉ 2024 Borislav Hadzhiev