Typeerror: str object is not callable – How to Fix in Python
Ihechikara Vincent Abba

Every programming language has certain keywords with specific, prebuilt functionalities and meanings.
Naming your variables or functions after these keywords is most likely going to raise an error. We’ll discuss one of these cases in this article — the TypeError: ‘str’ object is not callable error in Python.
The TypeError: ‘str’ object is not callable error mainly occurs when:
- You pass a variable named str as a parameter to the str() function.
- When you call a string like a function.
In the sections that follow, you’ll see code examples that raise the TypeError: ‘str’ object is not callable error, and how to fix them.
Example #1 – What Will Happen If You Use str as a Variable Name in Python?
In this section, you’ll see what happens when you used a variable named str as the str() function’s parameter.
The str() function is used to convert certain values into a string. str(10) converts the integer 10 to a string.
Here’s the first code example:
In the code above, we created a variable str with a value of «Hello World». We passed the variable as a parameter to the str() function.
The result was the TypeError: ‘str’ object is not callable error. This is happening because we are using a variable name that the compiler already recognizes as something different.
To fix this, you can rename the variable to a something that isn’t a predefined keyword in Python.
Here’s a quick fix to the problem:
Now the code works perfectly.
Example #2 – What Will Happen If You Call a String Like a Function in Python?
Calling a string as though it is a function in Python will raise the TypeError: ‘str’ object is not callable error.
Here’s an example:
In the example above, we created a variable called greetings .
While printing it to the console, we used parentheses after the variable name – a syntax used when invoking a function: greetings() .
This resulted in the compiler throwing the TypeError: ‘str’ object is not callable error.
You can easily fix this by removing the parentheses.
This is the same for every other data type that isn’t a function. Attaching parentheses to them will raise the same error.
So our code should work like this:
Summary
In this article, we talked about the TypeError: ‘str’ object is not callable error in Python.
TypeError: 'str' object is not callable
P.S I searched the site but it seams that all the people with the same problem had far more advanced scripts and I did not understand anything.
3 Answers 3
The first error is in the line
The result of lower() is a string, and parentheses after that mean that the object on the left (the string) gets called. Therefore, you get your error. You want
does not what you expect. Again, ans is a string, and parentheses mean that the object on the left (the string) gets called. Therefore, you get your error.
Also, or is a logical operator. Even after removing the parentheses after ans , the code gets evaluated as:
Since a non-empty string ( ‘no’ ) evaluates to the boolean value True, this expression is always True. You simply want
Python typeerror: ‘str’ object is not callable Solution
One of the most common errors in Python programming is typeerror: ‘str’ object is not callable, and sometimes it will be painful to debug or find why this issue appeared in the first place.
What is typeerror: ‘str’ object is not callable in Python?
Python has a built-in method str() which converts a specified value into a string. The str() method takes an object as an argument and converts it into a string.
Since str() is a predefined function and a built-in reserved keyword in Python, you cannot use it in declaring it as a variable name or a function name. If you do so, Python will throw a typeerror: ‘str‘ object is not callable.
Let us take a look at few scenarios where you could reproduce this error.
Scenario 1 – Declaring a variable name called “str”
The most common scenario and a mistake made by developers are declaring a variable named ‘ str ‘ and accessing it. Let’s look at a few examples of how to reproduce the ‘str’ object is not callable error.
In this example, we have declared ‘ str ‘ as a variable, and we are also using the predefined str() method to concatenate the string.
The above code is similar to example 1, where we try to convert integer x into a string. Since str is declared as a variable and if you str() method to convert into a string, you will get object not callable error.
Solving typeerror: ‘str’ object is not callable in Python.
Now the solution for both the above examples is straightforward; instead of declaring a variable name like “str” and using it as a function, declare a more meaningful name as shown below and make sure that you don’t have “str” as a variable name in your code.
Scenario 2 – String Formatting Using %
Another hard-to-spot error that you can come across is missing the % character in an attempt to append values during string formatting.
If you look at the below code, we have forgotten the string formatting % to separate our string and the values we want to concatenate into our final string.
In order to resolve the issue, add the % operator before replacing the values («World»,»a beautiful») as shown above.

report this ad
[Solved] TypeError: ‘str’ object is not callable

So you have encountered the exception, i.e., TypeError: ‘str’ object is not callable. In the following article, we will discuss type errors, how they occur and how to resolve them.
What is a TypeError?
The TypeError occurs when you try to operate on a value that does not support that operation. The most common reason for an error in a Python program is when a certain statement is not in accordance with the prescribed usage. The Python interpreter immediately raises a type error when it encounters an error, usually along with an explanation. For instance:
When you try to divide an integer value with a string, it results in a type error.

Why is the ‘str’ object not callable?
Like any TypeError, the ‘str’ object is not callable occurs when we try to perform an operation not supported by that datatype. In our case, here it’s the ‘string’ datatype. Let’s see some examples of where it can occur and how we can resolve them.
Example 1:
In the code above, we have deliberately defined the first variable name with a reserved keyword called str. Moreover, the second variable, string2, follows Python’s naming conventions. In the print statement, we use the str method on the str variable, this confuses the Python’s interpreter, and it raises a type error, as shown in the image below.

Example 2:
Contrary to example 1, where we had defined a variable with the str reserved keyword, which raised an error. In the example 2 code, we are using the ‘ % ‘ operator to perform string formatting. However, there is a minor issue in the syntax. We have omitted the % before the tuple. Missing this symbol can raise a similar error.

How to resolve this error?
The solution to example 1:
In Python, the str() function converts values into a string. It takes an object as an argument and converts it into a string. The str is a pre-defined function and also a reserved keyword. Therefore, it can’t be used as a variable name as per Python’s naming conventions.
To resolve the error, use another variable name.
The solution to example 2:
In example 2, we tried to use string formatting to display string1 and string2 together. However, not using the % operator before the tuples of values TypeError: ‘str’ object is not a callable error that gets thrown. To resolve it, use the % operator in its proper place.
TypeError: ‘str’ object is not callable Selenium
This error is likely due to accidentally invoking a function() which is actually a property. Please check your code for this.
TypeError ‘str’ object is not callable BeautifulSoup/Django/Collab
Irrespective of the library used, this error can creep in if you have used some reserved keyword as a variable or redefined some property or a function of the library. Please check your code for such things.
TypeError ‘str’ object is not callable matplotlib
Somewhere in the code, you might have used plt.xlabel = “Some Label”. This will change the import of matplotlib.pyplot. Try to close the Notebook and restart your Kernel. After the restart, rerun your code, and everything should be fine.
To fix this error, you need to ensure that no variable is named after the str reserved keyword. Change it if this is the case.
TypeError generally occurs if an improper naming convention has been used, in other words, if the str keyword is used as a variable name.
This error is likely due to accidentally overwriting one of the PySpark functions with a string. Please check your code for this.
Conclusion
In this article, we discussed where the ‘str’ object is not callable error can occur and how we can resolve it. This is an easy error to fix. We must ensure we don’t use the reserved keywords for naming variables. In addition, we also have to avoid redefining default methods.