‘int’ object is not subscriptable is a Python type error which occurs when you try to access “index” on int variables.
a = 1 print(str(a))
This code will throw,
'int' object is not subscriptable error because here we have assigned
a = 1 which means
a is an integer. In the next line, we are printing the 0th index of
print(str(a)), which is incorrect.
The error screen will look like this –
Arrays needs to be defined explicitly because, internally, they are not a value else the location of values. It means an array variable holds the address in memory location from where your first element starts.
So, the right way of implementing the above code is –
a = ['Ironman', 'Captain America', 'Thor', 'Hulk'] print(str(a)) // Output: Ironman
Python will throw the same error if you try to access a multidimensional index from single dimensional array. Or more precisely, large dimensional index than the maximum array dimension.
a = ['Ironman', 'Captain America', 'Thor', 'Hulk'] print(str(a)) // Error: 'int' object is not subscriptable
The live demo might not work in private window.