How To Fix Python Error Typeerror: ‘str’ Object Is Not Callable

When i develop python source code, i want to use python built-in function str() to conver a number variable to string type. But i meet below error Typeerror: ‘str’ Object Is Not Callable. 

>>> x = 1.99
>>>
>>> str(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

This error confused me a lot for some time. But i finally fix it after google it. The reason for this error is because i had use a variable name str in the same python interpreter, and i had assign string value to that variable str. So the python interpreter treat str as a variable name other than built-in function name str(). So it throw out TypeError: ‘str’ object is not callable. Below is the full python source  code.

>>> global str
>>>
# some times ago, i assign string 'hello' to str variable, so python interpreter think str is a string variable.
>>> str = 'hello'
>>>
......
......
......
>>> x= 1.99
# so when i invoke str() function, the interpreter think str is a string variable then error happened.
>>> str(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

Conclusion : Do not declare python variable name same with python built-in function name, keyword etc.

READ :   What Is The Difference Between R & U Flag At Python String Beginning

3 Comments


  1. but how did you fix it?? will the interpreter continue thinking str as a variable

    Reply

    1. try to run below and retry the code
      del str

      it will work now.

      Reply

  2. try to run below and re try the code
    del str

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.