If you are working with Python, you may encounter the error message “TypeError: ‘int’ object is not subscriptable” when trying to access an element of an integer variable using square brackets. This error occurs because integers are not subscriptable, meaning you cannot access individual elements of an integer like you can with a list or a string.

In this tutorial, we will explore the causes of this error and provide several solutions to fix it. We will also discuss some best practices to avoid this error in the future. So, let’s get started!
Understanding the TypeError: 'int' object is not subscriptable
error
The error message here is quite helpful. It tells us that we’re trying to use an integer as a subscriptable object (for example, a list, string, tuple, etc.) using the square brackets notation []
to retrieve a specific element or a slice of elements. An integer object represents an integer value and not a sequence and thus, we cannot really use them as subscriptible objects. If you try to do so, you’ll get this error.
Let’s look at an example.
# integer variable score = 91 # using integer as a subscriptable object print(score[0])
Output:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 4 2 score = 91 3 # using integer as a subscriptable object ----> 4 print(score[0]) TypeError: 'int' object is not subscriptable
In the above example, we created an integer variable score
that stores the integer value 91
. We then tried to access the value at index 0 in the variable score
and we get the error TypeError: 'int' object is not subscriptable
.
You’ll get the same if you try to perform a slice operation on an integer value.
# integer variable score = 91 # using integer as a subscriptable object print(score[0:3])
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 4 2 score = 91 3 # using integer as a subscriptable object ----> 4 print(score[0:3]) TypeError: 'int' object is not subscriptable
In the above scenarios, we are using an integer as a subscriptable object which is not allowed (and doesn’t really make sense) since an integer represents a single value and not a sequence of values and thus a TypeError
is raised.
Fixing the error
To fix this error, you can either not use integer objects as a subscriptable object or use a subscriptable object like a list or a string instead.
Let’s revisit the examples from above and fix those errors.
# integer variable score = 91 # print the integer print(score)
Output:
91
Here, we are not using the integer value as subscriptable object and instead directly printing its value. You can see that we don’t get an error.
# list of scores scores = [91, 77, 82, 45, 89] # using list as a subscriptable object print(scores[0:3])
Output:
[91, 77, 82]
Here, instead of using an integer value, we are using a list that is subscriptable, and thus performing operations like slicing or accessing a value at an index won’t result in this TypeError
.
Another use case could be you are trying to get the digits in a number. In that case, convert the integer to a string object (which is subscriptable) and then perform the required operations. Let’s look at an example.
# integer variable num = 12345 # integer to string num_str = str(num) # get the first digit print(num_str[0]) # get the last digit print(num_str[-1]) # get the first three digits print(num_str[:3])
Output:
1 5 123
Note that the solution to correct this error will depend on your use case and what you’re trying to achieve.
Conclusion
The “TypeError: ‘int’ object is not subscriptable” error occurs when we try to access an index of an integer or slice an integer, which is not possible as integers are not subscriptable. To fix this error, you can choose to not perform such operations on integers or use a subscriptable type like a list or a string instead.
You might also be interested in –