In Python, there are numbers of reserved keywords such as “str()” that are used to perform specific tasks in the program. The “str()” is the name of a predefined function that is not used anywhere else in the program. Sometimes, when using the “str()” function, the compiler gets confused between the string variable and the “str()” function, which invokes the error “TypeError: str object is not callable”. This Python write-up will provide various causes and solutions of “TypeError: str object is not callable” using the following aspects:
- Reason 1: Calling String as a Function
- Solution 1: Remove the Parentheses
- Solution 2: Use Square Bracket
- Reason 2: Overriding Built-in str() Function
- Solution: Rename the Variable
- Reason 3: Using the Same Name for Class Method and Class Attribute
- Solution: Rename Class Method
Reason 1: Calling String as a Function
The prominent reason which causes this “TypeError” is when a user tries to call the “string” variable as a function in Python.
The above snippet shows TypeError when the “string” value is accessed as a function.
Solution 1: Remove the Parentheses
To resolve this error, remove the parentheses and access the string value by placing the string variable inside the parentheses of the “print()” function.
Code:
string_value = 'Python Guide'
print(string_value)
In the above code, the string is created and displayed on the console screen using the “print()” function.
Output:
The above output successfully accessed the string value by removing parentheses.
Solution 2: Use Square Bracket
In Python, the “str object is not callable” is also resolved using square brackets instead of parentheses. The square bracket with a specific value returns the index value of the string. The following code example will help you better understand:
Code:
string_value = 'Python Guide'
print(string_value[7])
print(string_value[0:7])
In the above code, the string is created and accessed using the index value placed inside the square bracket. The particular integer value inside the square bracket returns the index value of the string. The “string slicing” method is also used to return the range of substring values.
Output:
The above output shows the string value at index “7” and the value of the substring from index range “0-7” using square brackets.
Reason 2: Overriding Built-in str() Function
The error also occurs in the program when the user overrides the built-in “str()” function.
An example of this is shown in the below snippet:
The above output shows the “TypeError” when the “str()” function is overridden in the program.
Solution: Rename the Variable
To resolve this error, rename the string variable and re-execute the program. The error will be resolved because the “str()” function will not override after renaming the string variable.
Code:
str_1 = 'hello world'
print(str(43))
In the above code, the “str” variable has been renamed as “str_1”. The built-in “str()” function accepts the integer value as an argument and converts it into a string without error.
Output:
The above output shows the numeric string converted using the “str()” function.
Reason 3: Using the Same Name for Class Method and Class Attribute
Another reason which causes the “str object is not callable” error in Python is using the same name for the class method and its attribute. Here is an example:
The above snippet shows “TypeError” because the given class method has a similar name to the class attribute.
Solution: Rename Class Method
To resolve this error, rename the class method and re-execute the program. Here is an example of code:
Code:
class sample():
def __init__(self, example):
self.example = example
def name(self):
return self.example
sp = sample('Alice')
print(sp.name())
In the above code, the class “sample” is created in the program. The class constructor function has an attribute named “examples”. The class method name changed from “examples” to “name” to avoid errors.
Output:
The above output successfully shows the name of class attributes without any error. The class method and attributes have been initialized with different names.
Conclusion
The error named “TypeError: str object is not callable” appears in Python when a user tries to call string as a function or override the built-in “str()” function. The error also occurs when the defined class contains the same name for its attributes and the same name for its method. To resolve this “TypeError”, various solutions can be followed, such as removing parentheses, using square brackets, renaming the variable, and renaming the class method. The index position is passed inside the square bracket to access the specified value of the string. This Python blog post presented a detailed guide on various causes and solutions for the “str object is not callable” error in Python.