Author -  Sai gowtham

Fix the 'str' object is not callable error in Python

In this tutorial, we are going to learn how to fix the TypeError: ‘str’ object is not callable in Python.

This error occurs for one of the following reasons:

  1. If you try to use an str() built-in function as a variable name.

  2. Using the parenthesis () instead of square brackets [] to access the characters from a string at a specific index.

Example:

str = 'Hello' # using str as a variable name

print(str(2)) # using parenthesis to access characters

Output:

TypeError:'str' object is not callable

To fix the error, use the square brackets [] syntax to access the characters from a list, and do not use the str keyword as a variable name because it is reserved for the str() function in python.

Here is an example:

greet = 'Hello'

print(greet[0]) # H'
print(greet[2]) # 'e'

Another cause of the error is calling the string value like a function :

greet_str = 'Hello'

# TypeError: 'str' object is not callable
print(greet_str())

Conclusion

The “str” object is not a callable error that occurs when we try to use the str as a variable name and using the parenthesis () to access the characters from a string. To fix the error always use the square brackets [] to access the characters at a specific index eg: user_str[1] and do not use the str() built-in keyword as a variable name because it is reserved for str() function in python.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY