In Python, a string object is a collection of characters inside the single, double or triple inverted comma. And when we try to call a string object as a function using parenthesis, we encounter the
TypeError: 'str' object is not callable
error.
In this Python tutorial, we will discuss the
TypeError: 'str' object is not callable
Error and how to debug it. We will also walk through a common example when many new python learners commit the mistake and encounter this error. So let’s get started with the Error statement.
Python Error: TypeError: 'str' object is not callable
The Error Statement TypeError: 'str' object is not callable is divided into two parts separated with a colon “:”.
- Exception Type (TypeError)
- Error Message (‘str’ object is not callable)
1. Exception Type (
TypeError
)
TypeError is a common Python standard exception, and it occurs in Python when we try to perform an unsupported operation on a Python object. Performing an addition operation between a string and integer is a common TypeError example.
2. Error Message (
‘str’ object is not callable
)
In Python string objects are represented as
str
object. For example, when we print the type of a string object we get ‘str’ class
# string
name ="Rahul"
print(type(name))
output
<class 'str'>
And when we try to access a string object or variable as a function call statement, we encounter a
TypeError
, with
str
object is not callable
Error Message.
Error Example
# string
name ="Rahul"
# accessing name string as a function
print(name())
Output
Traceback (most recent call last):
File " main.py", line 5, in <module>
print(name())
TypeError: 'str' object is not callable
Break the code
In the above example, the
name
is a string variable, and in
line 5
we are using the
name
variable as a function call statement by putting the parenthesis
()
. And when the Python interpreter tries to execute the
name()
statement it realises that
name
is a string variable and it can not be used as a function calling statement, that’s why it threw the error.
Solution
To solve the above example we only need to remove the parenthesis after the name variable.
Example Solution
# string
name ="Rahul"
# accessing name string
print(name)
Output
Rahul
Common Example Scenario
While writing a program, errors are inevitable. And one of the most common mistakes that many new Python learners do is they use a reserved keyword as a variable name to the object. And when they need to use that reserved keyword in the program, they encounter the error like TypeErorr: ‘str’ object is not callable.
The most common case when many Python learners get this error is when they use the str as a variable name in the program, and also try to convert a variable to a string object using the built-in
str()
function.
Example
str = input("Enter your DOB Year: ")
current_year = 2021
# calculate age
age = current_year - int(str)
# convert the age into string
message = "Your age is "+ str(age)
print(message)
Output
Enter your DOB Year: 1999
Traceback (most recent call last):
File " main.py", line 8, in <module>
message = "Your age is "+ str(age)
TypeError: 'str' object is not callable
Break the Code
In this example, we are getting the error in
line 8
with
message = "Your age is "+ str(age)
statement. And the error message is showing that
'str' object is not callable
. The Python does support an
str()
function that can convert an integer or float value to a string value. But in our program, it does not seem to work. This is because in line 2 we have defined a variable by name
str
, that accepts the input value from the user.
With that, Python now treats the
str
name as a string value, not a built-in function. And when we tried to use the inbuilt
str
function to convert the integer
age
value to string value we received the error.
Solution
Whenever we write
a program
in Python we should never consider using a registered keyword or function name as a new variable. To solve the above program all we need to do is change the name of
str
variable to something else which is not a reserved keyword and provide more meaning to the variable.
Example Solution
str_age = input("Enter your DOB Year: ")
current_year = 2021
# calculate age
age = current_year - int(str_age)
# convert the age into string
message = "Your age is "+ str(age)
print(message)
Output
Enter your DOB Year: 1999
Your age is 22
Final Thoughts!
With practice and the skill set of reading Python errors, you can easily find and debug this error. The Error TypeError: 'str' object is not callable only occurred in a Python program when you put the parenthesis just after the string value or variable name. Debugging this error is also very easy, the straightforward technique is to remove the parenthesis or check if you have defined a string variable name as the same name of the Python function.
People are also reading:
- Python TypeError: ‘float’ object cannot be interpreted as an integer Solution
- Python TypeError: ‘method’ object is not subscriptable Solution
- Python TypeError: ‘NoneType’ object is not callable Solution
- Python ValueError: invalid literal for int() with base 10: Solution
- Python Error: TypeError: ‘tuple’ object is not callable Solution
- Python TypeError: ‘function’ object is not subscriptable Solution
- Python Multiple Inheritance
- Class and Objects in Python
- Exception Handling
- Python User-defined Exception
Leave a Comment on this Post