Last updated: Apr 8, 2024
Reading timeยท4 min
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.
str()
functionHere is one example of how the error occurs.
# ๐๏ธ Overriding the built-in str() str = 'bobbyhadz.com' # โ๏ธ 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, rename your variable and restart your script.
# โ 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.
If you are trying to access the string at a specific index, use square brackets
[]
, not parentheses ()
.
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.
my_str = 'bobbyhadz.com' print(my_str[0]) # ๐๏ธ b print(my_str[1]) # ๐๏ธ o print(my_str[2]) # ๐๏ธ b
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.
my_str = 'bobbyhadz.com' print(my_str[0:3]) # ๐๏ธ bob print(my_str[0:5]) # ๐๏ธ bobby
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.
The error occurs for multiple reasons:
()
instead of square brackets
[]
.Make sure you don't have a function and a variable with the same name.
def example(): return 'bobbyhadz.com' 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.
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.
Another common cause of the error is simply trying to call a string as a function.
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.
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.
The error is also caused when we have a class method and a class property with the same name.
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.
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.
Here is another example of how the error occurs.
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.
def get_str(): return 'bobbyhadz.com' print(get_str()) # ๐๏ธ bobbyhadz.com
Now we only call the function once and print the result.
To solve the "TypeError: 'str' object is not callable" error, make sure:
()
instead of square
brackets []
.