How to Solve TypeError: ‘str’ object is not callable in Python

To solve TypeError: ‘str’ object is not callable in Python, do not use the variable name as the built-in keywords. The TypeError will be raised when you use the reserved keyword used by the Python interpreter.

The ‘str’ object is not callable is a TypeError that can be raised when:

  1. For example, a variable will be named str.
  2. And then, we will call the str() function to convert the data into a string.

    The problem here is that str is a built-in Python function, and that name should not be used as a variable name. But if we do this, the error is raised. You should not use any built-in function name as the variable name to solve this error. We should not use keywords as the variable name.

    Example

    str = "Hello"
    str1 = "World"
    
    print(str(str+str1))

    Output

    TypeError: 'str' object is not callable

    In this example, we can see that an error will be raised called ‘str’ object is not callable. Manually, we can say that str is a variable with data Hello. And str1 is the variable with the data world.

    While printing, we concatenate str and str1, making the result as str. But the compiler cannot understand that. The compiler thinks that str is a variable, and the user is passing a value inside the variable str.

    If you pass inside a parenthesis, then it is considered a function. So the program searches for the function with the name str. But the compiler cannot find a function. So instead, there is a variable named str.

    So it tries to pass the value inside a variable. Hence the operation cannot be performed. Thus the error is raised.

    To solve the TypeError problem, we should not use the variable name as the built-in keywords. However, if we change the variable name, the program works well.

    str1 = "Hello"
    str2 = "World"
    
    print(str(str1+str2))

    Output

    HelloWorld

    The program gets executed successfully, and the result is displayed as HelloWorld. 

    Another instance where this error occurs is while using string formatting. We would have missed the % symbol in string formatting.

    print("Hello %s have a %s" ("Readers", "nice day"))

    Output

    TypeError: 'str' object is not callable

    This program raises an error called the ‘str’ object is not callable. This is because we have not used the % symbol before (“Readers”, “nice day”).

    print("Hello %s have a %s" %("Readers", "nice day"))

    Output

    Hello Readers have a nice day

    Hence, The TypeError: ‘str’ object is not callable is a simple error that can be easily rectified, but we have to know what the cause for this error is.

    Conclusion

    This type of TypeError is raised when,

    1.  When we use a string formatting technique, check whether the % symbol is added. 
    2. Check whether any variable is named with the built-in keywords. For example, check whether any variable is called str. If it is named as str, then change the variable name.

    See also

    TypeError: must be str, not int

    TypeError: ‘float’ object is not iterable

    TypeError: list indices must be integers or slices, not str

    FloatingPointError in Python

    TypeError: Only size-1 arrays can be converted to Python scalars

    Leave a Comment