How to Resolve Python "TypeError: 'str' object is not callable"
The TypeError: 'str' object is not callable is a common error in Python that occurs when you attempt to use a string value as if it were a function, usually by adding parentheses () after it. Strings store textual data and are not executable functions or methods themselves.
This guide explains the typical scenarios that lead to this error and provides clear solutions for each.
Understanding the Error: Callable vs. Non-Callable Objects
In Python, parentheses () placed after an object signify a call operation, meaning you're trying to execute that object (like a function or method). Objects that can be executed this way are called "callables". Data types like strings (str), integers (int), lists (list), etc., are generally not callable; they hold data. Attempting to "call" a non-callable object, such as a string, results in a TypeError.
my_string = "hello"
print(type(my_string)) # Output: <class 'str'>
try:
# ⛔️ TypeError: 'str' object is not callable
# Trying to "call" the string "hello"
my_string()
except TypeError as e:
print(e)