If you are dealing with Python, you might experience the mistake message “TypeError: ‘int’ item is not subscriptable” when attempting to access a component of an integer variable making use of square braces. This mistake happens due to the fact that integers are not subscriptable, implying you can not access private components of an integer like you can with a checklist or a string.


In this tutorial, we will certainly check out the reasons for this mistake and also give numerous options to repair it. We will certainly likewise talk about some ideal techniques to prevent this mistake in the future. So, allow’s get going!
Understanding the TypeError: 'int' item is not subscriptable
error
The mistake message right here is fairly valuable. It informs us that we’re attempting to make use of an integer as a subscriptable item (as an example, a checklist, string, tuple, and so on) making use of the square braces symbols []
to recover a details aspect or a piece of components. An integer item stands for an integer worth and also not a series and also therefore, we can not truly utilize them as subscriptible things. If you attempt to do so, you’ll obtain this mistake.
Let’s take a look at an instance.
# integer variable rating = 91 # making use of integer as a subscriptable item print( score[0])
Output:
---------------------------------------------------------------------------. TypeError Traceback (newest telephone call last). Cell In[3], line 4. 2 rating = 91. 3 # making use of integer as a subscriptable item. -- > 4 print( score[0]). TypeError: 'int' item is not subscriptable
In the above instance, we developed an integer variable score
that keeps the integer worth 91
. We after that attempted to access the worth at index 0 in the variable score
and also we obtain the mistake TypeError: 'int' item is not subscriptable
.
You’ll obtain the exact same if you attempt to do a piece procedure on an integer worth.
# integer variable. rating = 91. # making use of integer as a subscriptable item. print( score[0:3])
Output:
---------------------------------------------------------------------------. TypeError Traceback (newest telephone call last). Cell In[4], line 4. 2 rating = 91. 3 # making use of integer as a subscriptable item. -- > 4 print( score[0:3]). TypeError: 'int' item is not subscriptable
In the above situations, we are making use of an integer as a subscriptable item which is not enabled (and also does not truly make good sense) given that an integer stands for a solitary worth and also not a series of worths and also therefore a TypeError
is increased.
Repairing the error
To repair this mistake, you can either not make use of integer things as a subscriptable item or make use of a subscriptable item like a checklist or a string rather.
Let’s take another look at the instances from above and also deal with those mistakes.
# integer variable. rating = 91. # publish the integer. print( rating)
Output:
91
Right Here, we are not making use of the integer worth as subscriptable item and also rather straight publishing its worth. You can see that we do not obtain a mistake.
# checklist of ratings. ratings = [91, 77, 82, 45, 89]. # making use of checklist as a subscriptable item. print( scores[0:3])
Output:
[91, 77, 82]
Below, as opposed to making use of an integer worth, we are making use of a checklist that is subscriptable, and also therefore executing procedures like cutting or accessing a worth at an index will not lead to this TypeError
.
An additional usage situation can be you are attempting to obtain the numbers in a number. Because situation, transform the integer to a string item (which is subscriptable) and after that do the called for procedures. Allow’s take a look at an instance.
# integer variable. num = 12345. # integer to string. num_str = str( num). # obtain the initial number. print( num_str[0]). # obtain the last number. print( num_str[-1]). # obtain the initial 3 numbers. print( num_str[:3])
Output:
1. 5. 123
Note that the service to fix this mistake will certainly depend upon your usage situation and also what you’re attempting to accomplish.
Conclusion
The “TypeError: ‘int’ item is not subscriptable” mistake happens when we attempt to access an index of an integer or cut an integer, which is not feasible as integers are not subscriptable. To repair this mistake, you can select to not do such procedures on integers or make use of a subscriptable kind like a checklist or a string rather.
You may likewise want–