Solve TypeError: ‘int’ object is not subscriptable in Python

While we are using the operator on a string there an integer value will raise TypeError. We can say it in other words as the TypeError is raised on an unsupported object type when we are applying an operation of the wrong type. This error generally occurs when we are trying to use the integer type value as an array.

In python language when any type of object that implements the get_item method then the class definition is being called subscriptable objects and when we are applying the method by which we can access the elements of the object for getting the value.

In this article, we are going to see what is the reason for this error and how can we overcome this type of error in programs.

Reasons for Error

Case 1:

Suppose we want to write a program to calculate your number for the car which is based on the date of joining a company.

In the above example, we have written a variable named date_of_joining which is in the format of dd/mm/yyyy and then we will be adding up the digits of total. Like there is an example:

Total addition is add of month digits, day digits, and year digits.

Then car_number is the sum of the total sum.

It will throw an error called as int object is not subscriptable.

NameError: name ‘birthday’ is not defined and an error will occur at line 7 as the reason for the error is that the variable name add_all is a type of integer not a type of string. We cannot treat this variable as an array like storage for numbers and we should try to access each number. To fix this error, I will convert the add_all  variable to a string value and then we will use the str for finding the digits.

String type is a container like that we can access each character of string using index access.

Here is an example:


Case 2:

Suppose we want to write a program in which we print product price as an array.

In the given code, we have given the integer value as the variable name as  ‘Fruit_Price’, but in the print statement, we are trying to use it as an array. We will provide the user input fruit name and the fruit price. Now we will give the value of the fruit price to the variable x in the int format. Calculate the price after the discount.


Solutions for Error

Case 1:

To solve this error you have to avoid using integer values as an array. We have converted the value of birthday into an integer and this means that we cannot access it using indexing. So now birth date will be the type of string so that we can use slicing and indexing on the string variable as usual. Integers are usually not indexed as strings.

To solve the problem,  we can do one thing we will remove the int() statement from our code. The input() statement usually always returns a string value. We can also slice this string value by using the main code.

Lets recall our main input statement:

To remove the error we will put input instead of int and our code will work successfully. We will no longer trying to slice the integer because our code will not contain any int statement further. Instead “Birthday” is stored as a string. The string is now sliced using the slicing index.

Revised Code:


Case 2:

To solve this error we will not any integer as an array for any value. Earlier we have assigned an integer value to the variable ‘FruitPrice’, but we are trying to use it as an array in the print statement. When we are revising the code then we are removing the variable x=0 and then we are giving it x=int(FruitPrice) and after giving the value of product price to the variable x in int format and then calculating the price after discount .after all this we are printing the output.

Recessing the code after the change as:



We have to use a sensible and a meaningful variable name when we are giving in the code. We should always give other name of the variables so that will tell about the data they look on. We should never use a variable name as same as python in-built function name, module name, and constants. The false value “TypeError: int object is not subscriptable” error is being arised as because when you access an integer as if it will only give a particular subscriptable object just like a list or a dictionary etc.

The error is saying that when we are treating an integer which will be a whole number as like a subscriptable object. Likely integers are not subscriptable objects as only objects are that contain other objects like strings, lists, tuples, and dictionaries are subscriptable. We cannot use the same syntax on a non-subscripatble value like a float or an integer as lists are subscriptable which means we can only use indexing to retrieve a value from a list.

Lastly when we are meeting this type of error then it is important to check many times so that to be accessed by index, the square bracket and make well sure when we are just using this against the container variables which will be done by such as containers. The false value can be only corrected by removing several types of indexing to find the values of the int object. Whenever it comes we need to apply indexing on the int object we first need to convert that into strings and then we will perform this method to solve all the issues.

Leave a Comment

Your email address will not be published. Required fields are marked *