3
#Dictionary Loop
d = {'key1':1,'key2':2}
for j in d:
    print(j, end=" ")
    print(j+" "+ str(d[j]))

Traceback (most recent call last): File "E:/Learning/Python/ComparisonAndControlFlow/ForLoop.py", line 22, in print(j+" "+ str(d[j])) TypeError: 'str' object is not callable*

1
  • can't reproduce the error from your code Commented Jun 5, 2016 at 18:13

1 Answer 1

1

You assigned the variable name str to something elsewhere in your code, shadowing the str built-in. A minimal reproduction would be:

>>> str = 'something'
>>> try_string = str(5)
TypeError: 'str' object is not callable

Python tries to use your assigned variable for the function call, resulting in your error. Find whatever you named str in your code, and rename it.

In general, always make sure you don't shadow built-ins with your variable names, to prevent this from happening.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.