How to Fix TypeError: 'Str' Object Is Not Callable in Python

TypeError: 'str' object is not callable occurs when an string value is called as a function and can be solved by renaming the str value. Here’s how.

Written by Reza Lavarian
Published on Aug. 14, 2024
Two developers reviewing a typeerror str object is not callable error on desktop
Image: Shutterstock / Built In
Brand Studio Logo

The Python TypeError: ‘str’ object is not callable occurs when you try to call a string value (str object) as if it was a function.

TypeError: ‘Str’ Object Is Not Callable Solved

TypeError: ‘Str’ object is not callable is thrown in Python when a string value is called as if it was a function. For example, when str is declared as a variable:

str = 'I am ' # ⚠️ str is no longer pointing to a function
age = 25

# ⛔ Raises TypeError: 'str' object is not callable
print(str + str(age))

The most common solution is to rename the str variable:

text = 'I am '
age = 25

print(text + str(age))
# Output: I am 25

Here’s what the error looks like:

Traceback (most recent call last):
  File "/dwd/sandbox/test.py", line 5, in 
    print(str + str(age))
                ^^^^^^^^
TypeError: 'str' object is not callable

Calling a string value isn’t something you’d do on purpose, though. It usually happens due to a wrong syntax or accidentally overriding a function name with a string value.

 

What Causes the TypeError: ‘Str’ Object Is Not Callable?

This TypeError occurs under various scenarios:

  1. Declaring a variable with a name that's also the name of a function.
  2. Calling a method that’s also the name of a property.
  3. Calling a method decorated with @property.

More on PythonHow to Fix TypeError: ‘List’ Object Is Not Callable in Python

 

How to Solve TypeError: ‘Str’ Object Is Not Callable in Python

Let’s examine how to solve each of the mistakes that can create the TypeError: ‘str’ object is not callable in Python.

Declaring a Variable With a Name That’s Also the Name of a Function 

A Python function is an object like any other built-in object in Python, such as str, int, float, dict and list, etc.

All built-in functions are defined in the builtins module and assigned a global name for easier access. For instance, calling str() invokes __builtins__.str() function internally.

That said, overriding a function, accidentally or on purpose, with a string value is technically possible. 

In the following example, we have two variables named str and age. We need to concatenate them and print them on the screen. To do that, we need to convert age to a string first by using the built-in str() function.

str = 'I am ' # ⚠️ str is no longer pointing to a function
age = 25

# ⛔ Raises TypeError: 'str' object is not callable
print(str + str(age))

If you run the above code, Python will produce this error because we’ve assigned str to another value (‘I am ’).

We have two ways to fix the issue:

  1. Rename the variable str.
  2. Explicitly access the str() function from the builtins module (__bultins__.str).

The second approach isn’t recommended, unless you're developing a module. For instance, if you want to implement an open() function that wraps the built-in open():

# Custom open() function using the built-in open() internally
def open(filename):
     # ...
     __builtins__.open(filename, 'w', opener=opener)
     # ...

In almost every other case, you should always avoid naming your variables as existing functions and methods. But if you’ve done so, renaming the variable would solve the issue.

So, the above example could be fixed like this:

text = 'I am '
age = 25

print(text + str(age))
# Output: I am 25

This issue is common with function names you’re more likely to use as variable names. Functions such as str, type, list, dir or user-defined functions.

In the following example, we declare a variable named len. And at some point, when we call len() to check the input, we’ll get an error:

len = '' # ⚠️ len is set to an empty string
name = input('Input your name: ')

# ⛔ Raises TypeError: 'str' object is not callable
if (len(name)):
print(f'Welcome, {name}')

To fix the issue, all we need is to choose a different name for our variable:

length = ''
name = input('Input your name: ')

if (len(name)):
print(f'Welcome, {name}')

Long story short, you should never use a function name, built-in or user-defined, for your variables.

Overriding functions and calling them later on is the most common cause of this TypeError, similar to calling integer numbers.

2. Calling a Method That’s Also the Name of a Property 

When you define a property in a class constructor, any further declarations of the same name, such as methods, will be ignored.

class Book:
    def __init__(self, title):
        self.title = title

    def title(self):
        return self.title

book = Book('Head First Python')

# ⛔ Raises "TypeError: 'str' object is not callable"
print(book.title())

In the above example, since we have a property named title, the method title() is ignored. As a result, any reference to title will return the property (a string value). And if you call title(), you’re actually trying to call a string value.

The name get_title sounds like a safer and more readable alternative:

class Book:
    def __init__(self, title):
        self.title = title

    def get_title(self):
        return self.title

book = Book('Head First Python')

print(book.get_title())
# Output: Head First Python
A tutorial on how to solve the TypeError: 'str' object is not callable. | Video: Code Fix

More on PythonTiming Function in Python: A Guide

3. Calling a Method Decorated With @property Decorator 

The @property decorator turns a method into a “getter” for a read-only attribute of the same name.

class Book:
    def __init__(self, title):
        self._title = title

    @property
    def title(self):
        """Get the book price"""
        return self._title

book = Book('Head First Python')

# ⛔ Raises "TypeError: 'str' object is not callable"
print(book.title())

You need to access the getter method without the parentheses:

class Book:
    def __init__(self, title):
        self._title = title

    @property
    def title(self):
        """Get the book price"""
        return self._title

book = Book('Head First Python')
print(book.title)
# Output: Head First Python

Problem solved.

Frequently Asked Questions

The TypeError: ‘str’ object is not callable indicates that a string value or str object was called as if it was a function. It’s typically due to a wrong syntax or accidentally overriding a function name with a string value. Here’s what it looks like:

 

Traceback (most recent call last):
  File "/dwd/sandbox/test.py", line 5, in 
    print(str + str(age))
                ^^^^^^^^
TypeError: 'str' object is not callable

The most common reason for the TypeError: ‘str’ object is not callable is when you override a function that’s also a string value. For example:

str = 'I am ' # ⚠️ str is no longer pointing to a function
age = 25

# ⛔ Raises TypeError: 'str' object is not callable
print(str + str(age)

The solution is to rename the variable str, using the following code:

text = 'I am '
age = 25

print(text + str(age))
# Output: I am 25
Explore Job Matches.