Select Page

How to Solve TypeError: ‘str’ object is not callable

by | Programming, Python, Tips

This error occurs when you try to call a string as if it were a function. This error can occur if you override the built-in str() function or you try to access elements in a string using parentheses instead of square brackets.

You can solve this error by ensuring you do not override the str() function or any function names. For example:

my_str = 'Python is fun!'

my_int = 15

my_int_as_str = str(15)

If you want to access elements in a string, use square brackets. For example,

my_str = 'Python is fun!'

first_char = my_str[0]

This tutorial will go through the error in detail, and we will go through an example to learn how to solve the error.

TypeError: ‘str’ object is not callable

What is a TypeError?

TypeError tells us that we are trying to perform an illegal operation for a specific Python data type.

What does Callable Mean?

Callable objects in Python have the __call__ method. We call an object using parentheses. To verify if an object is callable, you can use the callable() built-in function and pass the object to it. If the function returns True, the object is callable, and if it returns False, the object is not callable.

Let’s test the callable() built-in function with a string:

string = "research scientist"

print(callable(string))
False

We see that callable returns false on the string.

Let’s test the callable() function with the square root method from the math module:

from math import sqrt

print(callable(sqrt))
True

We see that callable returns True on the sqrt method. All methods and functions are callable objects.

If we try to call a string as if it were a function or a method, we will raise the error “TypeError: ‘str’ object is not callable.”

Example #1: Using Parenthesis to Index a String

Let’s look at an example of a program where we define a for loop over a string:

string = "research scientist"

for i in range(len(string)):

    print(string(i))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      1 for i in range(len(string)):
      2     print(string(i))
      3 

TypeError: 'str' object is not callable

To index a string, you have to use square brackets. If you use parentheses, the Python interpreter will treat the string as a callable object. Strings are not callable. Therefore you will raise the error “TypeError: ‘str’ object is not callable”.

Solution

We need to replace the parentheses with square brackets to solve this error.

string = "research scientist"

for i in range(len(string)):

    print(string[i])
r
e
s
e
a
r
c
h
 
s
c
i
e
n
t
i
s
t

The code runs with no error and prints out each character in the string.

Example #2: String Formatting Using

The TypeError can also occur through a mistake in string formatting. Let’s look at a program that takes input from a user. This input is the price of an item in a store with a seasonal discount of 10%. We assign the input to the variable price_of_item. Then we calculate the discounted price. Finally, we can print out the original price and the discounted price using string formatting.

price_of_item = float(input("Enter the price of the item"))

discount_amount = 0.1

discounted_price = price_of_item - (price_of_item * discount_amount)

rounded_discounted_price = round(discounted_price,2)

print('The original price was %s, the price after the discount is %s'(price_of_item, rounded_discounted_price))

With string formatting, we can replace the %s symbols with the values price_of_item and rounded_discounted_price. Let’s see what happens when we try to run the program.

Enter the price of the item17.99

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 print('The original price was %s, the price after the discount is %s'(price_of_item, rounded_discounted_price))

TypeError: 'str' object is not callable

The code returns an error because we forgot to include the % operator to separate the string and the values we want to add to the string. The Python interpreter tries to call ‘The original price was %s, the price after 10% discount is %s‘ because the string has parentheses following it.

Solution

To solve this error, we need to add the % between the string

‘The original price was %s, the price after the discount is %s‘ and (price_of_item, rounded_discounted_price)

price_of_item = float(input("Enter the price of the item"))

discount_amount = 0.1

discounted_price = price_of_item - (price_of_item * discount_amount)

rounded_discounted_price = round(discounted_price,2)

print('The original price was %s, the price after the discount is %s'%(price_of_item, rounded_discounted_price))
Enter the price of the item17.99

The original price was 17.99, the price after the discount is 16.19

The code successfully prints the original price and the rounded price to two decimal places.

Example #3: Using the Variable Name “str”

Let’s write a program that determines if a user is too young to drive. First, we will collect the current age of the user using an input() statement. If the age is above 18, the program prints that the user is old enough to drive. Otherwise, we calculate how many years are left until the user can drive. We use the int() method to convert the age to an integer and then subtract it from 18.

Next, we convert the value to a string to print to the console. We convert the value to a string because we need to concatenate it to a string.

str = input("What is your age? ")

if int(str) >= 18:

    print('You are old enough to drive!')

else:

    years_left = 18 - int(str)

    years_left = str(years_left)

    print('You are not old enough to drive, you have ' + years_left + ' year(s) left')


Let’s see what happens when we run the program:

What is your age? 17

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      3 else:
      4     years_left = 18 - int(str)
      5     years_left = str(years_left)
      6     print('You are not old enough to drive, you have ' + years_left + ' year(s) left')
      7 

TypeError: 'str' object is not callable

We raise the error “TypeError: ‘str’ object is not callable” because we tried to use the str() method to convert the integer value years_left. However, earlier in the program we declared a variable called “str“. From that point on, the Python interpreter sees “str” as a string in the program and not a function. Therefore, when we try to call str() we are instead trying to call a string.

Solution

To solve this error, we need to rename the user input to a suitable name that describes the input. In this case, we can use “age“.

age = input("What is your age? ")

if int(age) >= 18:

    print('You are old enough to drive!')

else:

    years_left = 18 - int(age)

    years_left = str(years_left)

    print('You are not old enough to drive, you have ' + years_left + 'year(s) left')

Now we have renamed the variable, we can safely call the str() function. Let’s run the program again to test the outcome.

What is your age? 17

You are not old enough to drive, you have 1 year(s) left

The code runs and tells the user they have 1 year left until they can drive.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on the ‘not callable’ TypeError, go to the article: How to Solve Python TypeError: ‘module’ object is not callable.

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!