“TypeError: ‘str’ object is not callable” Error Solution

Python provides different types like string. The str() function is used to convert provided types like integer, float, etc into the string type. If the str is also used as a variable or object name and we try to call the str() function we may get the “TypeError: ‘str’ object is not callable” error like below.

“TypeError: ‘str’ object is not callable” Error

The TypeError: 'str' object is not callable error is generally displayed like below where the error line is the last line.

“TypeError: ‘str’ object is not callable” Error

Solve By Renaming Variable Name

The most probable reason for this error is using the str as a variable name which overrides the str() function. And when we try to call the str() function actually the str object is tried to be called but as it is not a function it can not be called and the object is not callable exception is returned.

str = "hello"

print(str(20))

To solve this exception we should rename the variable str into a different name like mystr . In the following example, we solve the exception by renaming variable str into mystr. In the following example, we will not get any errors.

mystr = "hello"

print(str(20))

print(mystr)

Solve By Calling str() Function

Another case for the “TypeError: ‘str’ object is not callable” exception trying to call a variable like a function. In the following example if we create a variable as mystr and try to call it like a function mystr() we get the “TypeError: ‘str’ object is not callable” exception.

mystr = "hello"

print(mystr())

The solution is just not call the variable like a function.

Leave a Comment