How to Solve TypeError: ‘int’ object is not Subscriptable

Introduction

Some of the objects in Python are subscriptable. This means that they hold and hold other objects, but an integer is not a subscriptable object. We use Integers used to store whole number values in Python. If we treat an integer as a subscriptable object, it will raise an error. So, we will discuss the type of error we get while writing the code in Python, i.e., TypeError: ‘int’ object is not subscriptable. We will also discuss the various methods to overcome this error.

What is TypeError: ‘int’ object is not subscriptable?

What is TypeError?

The TypeError occurs when you try to operate on a value that does not support that operation. Let us understand with the help of an example:

Suppose we try to concatenate a string and an integer using the ‘+’ operator. Here, we will see a TypeError because the + operation is not allowed between the two objects that are of different types.

#example of typeError

S = "Latracal Solutions"
number = 4
print(S + number)

Output:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    print(S + number)
TypeError: can only concatenate str (not "int") to str

Explanation:

Here, we have taken a string ‘Latracal Solutions” and taken a number. After that, in the print statement, we try to add them. As a result: TypeError occurred.

What is ‘int’ object is not subscriptable?

When we try to concatenate string and integer values, this message tells us that we treat an integer as a subscriptable object. An integer is not a subscriptable object. The objects that contain other objects or data types, like strings, lists, tuples, and dictionaries, are subscriptable. Let us take an example :

1. Number: typeerror: ‘int’ object is not subscriptable

#example of integer which shows a Typeerror

number = 1500
print(number[0])

output:

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print(number[0])
TypeError: 'int' object is not subscriptable

Explanation:

Here, we have taken a number and tried to print the through indexing, but it shows type error as integers are not subscriptable.

2. List: typeerror: ‘int’ object is not subscriptable

This TyperError problem doesn’t occur in the list as it is a subscriptable object. We can easily perform operations like slicing and indexing.

#list example which will run correctly

Names = ["Latracal" , " Solutions", "Python"]
print(Names[1])

Output:

Solutions

Explanation:

First, we have taken the list of names and accessed it with the help of indexing. So, it shows the output as Solutions.

Daily Life Example of How typeerror: ‘int’ object is not subscriptable can Occur

Let us take an easy and daily life example of your date of birth, written in date, month, and year. We will write a program to take the user’s input and print out the date, month, and year separately.

#Our program begins from here

Date_of_birth = int(input("what is your birth date?"))

birth_date = Date_of_birth[0:2]

birth_month = Date_of_birth[2:4]

birth_year = Date_of_birth[4:8]

print(" birth_date:",birth_date)
print("birth_month:",birth_month)
print("birth_year:",birth_year)

Output:

what is your birth date?31082000
Traceback (most recent call last):
  File "C:/Users/lenovo/Desktop/fsgedg.py", line 3, in <module>
    birth_date = Date_of_birth[0:2] 
TypeError: 'int' object is not subscriptable

Explanation:

Here firstly, we have taken the program for printing the date of birth separately with the help of indexing. Secondly, we have taken the integer input of date of birth in the form of a date, month, and year. Thirdly, we have separated the date, month, and year through indexing, and after that, we print them separately, but we get the output ad TypeError: ‘int’ object is not subscriptable. As we studied above, the integer object is not subscriptable.

Solution of TypeError: ‘int’ object is not subscriptable

We will make the same program of printing birth data by taking input from the user. In that program, we have converted the date of birth as an integer, so we cannot perform operations like indexing and slicing.

To solve this problem now, we will remove the int() statement from our code and run the same code.

#remove int() from the input()

Date_of_birth = input("what is your birth date?")

birth_date = Date_of_birth[0:2]

birth_month = Date_of_birth[2:4]

birth_year = Date_of_birth[4:8]

print(" birth_date:",birth_date)
print("birth_month:",birth_month)
print("birth_year:",birth_year)

Output:

what is your birth date?31082000
 birth_date: 31
birth_month: 08
birth_year: 2000

Explanation:

Here, we have just taken the input into the string by just removing the int(), and now we can do indexing and slicing in it easily as it became a list that is subscriptable, so no error arises.

Must Read

Conclusion: Typeerror: ‘int’ object is not subscriptable

We have learned all key points about the TypeError: ‘int’ object is not subscriptable. There are objects like lists, tuples, strings, and dictionaries that are subscriptable. This error occurs when you try to do indexing or slicing in an integer.

Suppose we need to perform operations like indexing and slicing on integers. Firstly, we must convert the integer into a string, list, tuple, or dictionary.

Now, you can easily solve this Python TypeError like a smart coder.

However, please let me know in the comment section below if you have any questions. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments