Python TypeError: ‘int’ object is not subscriptable

In Python, we use Integers to store the whole numbers, and it is not a subscriptable object. If you treat an integer like a subscriptable object, the Python interpreter will raise TypeError: ‘int’ object is not subscriptable.

In this tutorial, we will learn what ‘int’ object is is not subscriptable error means and how to resolve this TypeError in your program with examples.

What is Subscriptable in Python?

Subscriptable” means that you’re trying to access an element of the object. The elements are usually accessed using indexing since it is the same as a mathematical notation that uses actual subscripts.

How do you make an object Subscriptable?

In Python, any objects that implement the __getitem__ method in the class definition are called subscriptable objects, and by using the  __getitem__  method, we can access the elements of the object.

For example, strings, lists, dictionaries, tuples are all subscriptable objects. We can retrieve the items from these objects using indexing.

Note: Python doesn't allow to subscript the NoneType if you do Python will raise TypeError: 'NoneType' object is not subscriptable

How to Fix TypeError: ‘int’ object is not subscriptable?

Let us take a small example to read the birth date from the user and slice the day, months and year values into separate lines.

birth_date = int(input("Please enter your birthdate in the format of (mmddyyyy) "))

birth_month = birth_date[0:2]
birth_day = birth_date[2:4]
birth_year = birth_date[4:8]

print("Birth Month:", birth_month)
print("Birth Day:", birth_day)
print("Birth Year:", birth_year)

If you look at the above program, we are reading the user birth date as an input parameter in the mmddyy format.

Then to retrieve the values of the day, month and year from the user input, we use slicing and store it into a variable.

When we run the code, Python will raise a TypeError: ‘int’ object is not subscriptable.

Please enter your birthdate in the format of (mmddyyyy) 01302004
Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 3, in <module>
    birth_month = birth_date[0:2]
TypeError: 'int' object is not subscriptable

Solution

 In our example, we are reading the birth date as input from the user and the value is converted to an integer. 

The integer values cannot be accessed using slicing or indexing, and if we do that, we get the TypeError. 

To solve this issue, we can remove the int() conversion while reading the input from the string. So now the birth_date will be of type string, and we can use slicing or indexing on the string variable.

Let’s correct our example and run the code.

birth_date = input("Please enter your birthdate in the format of (mmddyyyy) ")

birth_month = birth_date[0:2]
birth_day = birth_date[2:4]
birth_year = birth_date[4:8]

print("Birth Month:", birth_month)
print("Birth Day:", birth_day)
print("Birth Year:", birth_year)

Output

Please enter your birthdate in the format of (mmddyyyy) 01302004
Birth Month: 01
Birth Day: 30
Birth Year: 2004

The code runs successfully since the int() conversion is removed from the code, and slicing works perfectly on the string object to extract a day, month and year.

Conclusion

The TypeError: ‘int’ object is not subscriptable error occurs if we try to index or slice the integer as if it is a subscriptable object like list, dict, or string objects.

The issue can be resolved by removing any indexing or slicing to access the values of the integer object. If you still need to perform indexing or slicing on integer objects, you need to first convert that into strings or lists and then perform this operation.