When you are using python str() function, you may find TypeError: ‘str’ object is not callable error. In this tutorial, we will introduce you how to fix this error.
Look at example code below:
str = "https://www.tutorialexample.com" def printString(num): num = str(num) print(num) printString(2)
Then you will get this error.
Why does this error occur?
The reason is we have created a variable called str and set it a string value.
str = "https://www.tutorialexample.com"
The name of python variable is the same to the python str() function.
How to fix this error.
1.Don’t create a variable which are same to python built-in function name.
We can change the str variable to other name, such as data.
data = "https://www.tutorialexample.com"
2.Don’t create a file name which are same to python built-in function name.
For example, we should not create a str.py in your python project.
There are other reasons to occur this error. You can read this tutorial.
Fix Python TypeError: ‘str’ object is not callable – Python Tutorial