Typeerror: str object is not callable

The TypeError: 'str' object is not callable error occurs when you try to call a string object as if it were a function. This can happen due to several reasons, such as overwriting a built-in function with a string, having a function and a variable with the same name, or calling a function that returns a string twice. To fix this error, you need to identify the cause of the error and correct the issue.

Let's take a look at some examples of common causes for the TypeError: 'str' object is not callable error:

Attempting to call a string with parentheses

This error can occur if you try to call a string with parentheses as if it were a function.

greeting = "Hello, world!" print(greeting()) #Output:TypeError: 'str' object is not callable

In the above case, greeting is a string, not a function, so attempting to call it like a function will result in a TypeError.

The correct way to print the string would be:

greeting = "Hello, world!" print(greeting) #Output: Hello, world!

Overwriting a built-in function with a string

Another possible cause of this error is overwriting a built-in function with a string.

str = "Hello, world!" print(str(123)) #Output:TypeError: 'str' object is not callable

In the above case, str is a built-in function in Python that converts an object to a string. However, you have overwritten it with a string object, so attempting to call it like a function will result in a TypeError.

To fix this, you can simply choose a different name for our string variable:

my_str = "Hello, world!" print(str(123))

Using parentheses after a variable name

Also, this error can also occur if you accidentally put parentheses after a variable name, making Python think you are trying to call a function.

name = "Alice" print(name("Jones")) #Output:TypeError: 'str' object is not callable

In the above case, name is a string, not a function, so attempting to call it like a function will result in a TypeError.

To fix this, you can simply remove the parentheses:

name = "Alice" print(name + " Jones") #Output: Alice Jones

Having a function and a variable with the same name

If you have a function and a variable with the same name, it can cause the TypeError: 'str' object is not callable error. This is because when you call the name as a function, Python will try to use the variable instead of the function, resulting in a TypeError.


how to solve str object is not callable

For example, suppose you have a function called foo() and a variable with the same name:

def foo(): return "This is the function" foo = "This is the variable" print(foo())
#Output:TypeError: 'str' object is not callable

In the above case, when you try to call foo() as a function, Python will try to use the variable instead, resulting in a TypeError.

To fix this, you can rename the variable to something different:

def foo(): return "This is the function" foo_var = "This is the variable" print(foo())
#Output:This is the function

In the above updated code, renamed the variable to foo_var, which prevents the name conflict and allows you to call the function without errors.

Alternatively, you can also rename the function to something different if the variable is used more frequently or is critical to your program. In general, it's best practice to avoid using the same name for functions and variables to prevent these types of errors.