Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Str Object is not Callable; Python Dictionary Loop Error [duplicate]

#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*

like image 276
Asad Anwer Avatar asked Mar 28 '26 17:03

Asad Anwer


1 Answers

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.

like image 156
miradulo Avatar answered Mar 30 '26 08:03

miradulo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!