‘float’ object is not subscriptable is a Python type error which occurs when you try to access “index” on float variables.

a = 1.0
print(str(a[0]))

This code will throw, 'float' object is not subscriptable error because here we have assigned a = 1.0 which means a is a float value. In the next line, we are printing the 0th index of a, print(str(a[0])), which is incorrect.

The error screen will look like this –

In type unsafe languages like javascript, php, python etc., we can assign int to float or int to string or string to boolean but it doesn’t work for arrays.

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[0]))
// Output: Ironman

    Tweet this to help others

Live Demo

The live demo might not work in private window.

Tagged in:

,

Comments

About the Author

akamit

I am Akash Mittal, an overall computer scientist. If you want to guest post, need help in your projects, want to advertise, Feel free to contact me at [email protected]

View All Articles