Python TypeError: ‘int’ Object Is Not Subscriptable Fix

Python stores whole numbers in Integers, which are not subscriptable objects. TypeError: ‘int’ object is not subscriptable will be raised if you attempt to treat an integer as a subscriptable object.

The purpose of this tutorial is to explain what the error ‘int’ object is not subscriptable means, as well as how to resolve this TypeError in your program through the use of examples.

What does it mean if a Python object is “subscriptable”?

Subscriptable Object in Python is just indexing because it is the same as a mathematical notation that uses subscripts, which uses actual subscripts in Python. Array[0] is used in Python to get the first element of an array, while a0 is used by mathematicians for the same task.

Objects that can be subscripted are referred to as subscriptable objects in simple terms. Subscriptable data types in Python include strings, lists, tuples, and dictionaries. So sometimes you may get errors as the Object is not subscriptable.

Fix Python TypeError: ‘int’ Object Is Not Subscriptable

When a class definition includes the __getitem__ method, any objects that implement it are referred to as subscriptable objects. Subscriptable objects are objects that can have their elements accessed by executing the __getitem__ method.

It should be noted that Python does not permit subscripting the NoneType. If you do, Python will throw a TypeError: The ‘NoneType’ object cannot be subscripted.

#Testing the Error
phoneNumber = int(input("Enter your Phone Number with Area Code\n"))

areaCode = phoneNumber[0:4]
cellNum = phoneNumber[4:10]

print(areaCode)

Output:

Enter your Phone Number with Area Code
5412847364
Traceback (most recent call last):
  File "c:\Users\Coduber\test.py", line 4, in <module>
    areaCode = phoneNumber[0:4]
TypeError: 'int' object is not subscriptable

Here, we are reading the phone number entered by the user and converting it to an integer using the typecasting method.

And then we are performing Slicing and indexing on the integer value which is not allowed in Python. Since you can only do slicing or indexing to listicle objects.

When reading the input from the string, we can avoid performing the int() conversion, which will solve this problem. As a result, the birth date variable will now be of type string, and we will be able to slice and index the string variable as needed.

Let us see that the correct usage of the above program to fix the ‘int’ object is not a subscriptable error message.

#Testing the Error
phoneNumber = input("Enter your Phone Number with Area Code\n")

#Typecasting To Int After Slicing 
areaCode = int(phoneNumber[0:4])
cellNum = int(phoneNumber[4:10])

print("Area Code is {} and Cell Phone is {}".format(areaCode, cellNum))

Output:

Enter your Phone Number with Area Code
5412847364
Area Code is 5412 and Cell Phone is 847364

As you can see in the code above, I typecasted the numbers after slicing them from the string. As a result, you can avoid the error message you were receiving.

Python TypeError: ‘int’ Object Is Not Subscriptable Fix

Wrap Up

I hope you were successful in correcting the TypeError: The ‘int’ object is not subscriptable, which results in an error. When you try to perform indexing on an integer value or slicing on an integer value, this error usually occurs.

As a result, the best way to avoid such an error is to perform slicing or indexing on a string value rather than an integer value, as shown in the preceding code example.

Let me know in the comment section if you are still facing this error or you have a better solution than the one discussed here I will be happy to add it here or help you.

Further Read:

  1. Python iloc() function – Complete Guide
  2. Iterate Through A List In Python
  3. Exit A Python Program in 3 Ways
  4. How To Find A String In A List In Python

Leave a Comment