0

I have been trying to use str() function to convert the integer to string in Spyder (python 2.7). Every time I got TypeError: 'str' object is not callable

For example, I wrote this simple code to test it and I got the same error:

x = 5
print str(x)

Can someone help me in this

Nasser
  • 2,118
  • 6
  • 33
  • 56
  • 4
    You probably created another variable called `str`. If you run that code in a brand-new interpreter session, it shouldn't give that error. – BrenBarn Nov 13 '15 at 19:17

1 Answers1

2

You have overwritten the built-in str somewhere in your code.

>>> str = 'foo'   # overwriting the builtin `str`
>>> x = 5
>>> print str(x)  # equivalent to 'foo'(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
  • I checked my code and I didn't find str anywhere! is there another way to convert int to String? – Nasser Nov 13 '15 at 19:32
  • @Nasser: Check your code again. Do you import any modules like `from whatever import *`? If so check those modules as well. Also, before `print str(x)` do `print repr(str)`. That should help you figure things out. – Steven Rumbalski Nov 13 '15 at 19:37