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: 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. 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. Example #1 – What Will Happen If You Use str as a Variable Name in Python?
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:
greetings = "Hello World" print(str(greetings)) # Hello WorldNow 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:
greetings = "Hello World" print(greetings()) # TypeError: 'str' object is not callableIn 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:
greetings = "Hello World" print(greetings) # Hello WorldSummary
In this article, we talked about the TypeError: 'str' object is not callable error in Python.
We talked about why this error might occur and how to fix it.
To avoid getting this error in your code, you should:
- Avoid naming your variables after keywords built into Python.
- Never call your variables like functions by adding parentheses to them.
Happy coding!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
In today’s tutorial we’ll learn how to troubleshoot a common error typically encountered by newcomers to the Python programming language: trying to use a string containing numeric values as an integer. Look carefully into the code below (you can better follow along by copying this to you Python
development tool of choice (Idle, Spyder, Jupyter, PyCharm, VSCode etc’): This will obviously result in an type error. The input function receives a string. Our short program goes ahead and tries to interpret it as an integer – that won’t work. Here’s a screenshot from my Jupyter Lab Notebook: We can get rid of this type error by making a very simple modification to our program. We’ll cast the user input (which as we mentioned before, is a string) to an integer data type. Then
we will go ahead and use that integer as a range in our for loop. The changes to the program are demarcated in bold characters: This will work as expected. In our case list all numbers from 0 to 99 included.Problem – Str cannot be interpreted as int in Python
Solution – cast your input to use it in the loop
Additional learning