In Python, there are some subscriptable objects such as string, array, list, and tuples. All these objects are capable of holding a sequence of elements or objects. But objects like integers, and float are not subscriptable because they store a single value. And if we try to treat an integer value as a subscriptable object we receive a TypeError called
‘int’ object is not subscriptable
.
In this Python guide we will go through this most common error, and discuss why it occurs and how can you solve it?
Error Intro
Let's start with the error introduction, whenever the Python interpreter throws an error on the terminal or shell, the error we generally get in this format
ErrorType: Error Message
. And this error statement also follows the same syntax
typeerror: ‘int’ object is not subscriptable
.
The
typeerror
is a type of error, which generally represent the logic error, when we mishandle an object or data type with different functionality.
Performing an addition operation between a string number and an int number is a classic type error example.
‘int’ object is not subscriptable
is the error message that provides more specific detail about the error itself. And this error occurs when we perform the subscriptable operation on an integer value.
Subscriptable object in Python
In Python, there are 4 default subscriptable objects string , list , tuples , and dictionaries . So, what makes these all objects subscritable?
One thing is common with all of these Python data structures that are accessing an element. When we want to access an element from a string, list, tuple, or dictionary, we can use the variable name followed by the square bracket and an index or hash(key) number inside the bracket.
Example
# string
string = "0123"
# list
lis = [0,1,2,3]
# tuple
tup = (0,1,2,3)
# dictionary
dictionary = {0:0, 1:1, 2:2, 3:3}
# access string
print(string[0]) # 0
# aceess list
print(lis[0]) # 0
# acess tuple
print(tup[0]) # 0
# acess dictionary
print(dictionary[0]) # 0
All of these Python objects support a similar syntax for accessing elements what makes them subscriptable objects.
The Problem: typeerror: ‘int’ object is not subscriptable
Now we know what are subscriptable objects in Python, now let's discuss why we get the error
typeerror:‘int’ object is not subscriptable
. As we are receiving
typeerror,
this means we are mishandling one datatype property with another data type properties or features.
So, which data type property are we mishandling? The answer is in the error message
‘int’ object is not subscriptable
. According to the error message we are trying to perform the subscriptable operation on an integer object or data type.
Example
>>> num = 20 #num is an integer
>>> num[0] #perfrom indexing on an integer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
The error is quite obvious, we are receiving it because we are trying to perform the indexing or
subscriptable
operation on an integer number that is illegal in Python.
Common Scenario: TypeError: 'int' object is not subscriptable
The most common scenario when we encounter this error is when we input the data from the user using the input function and convert it into an integer. But later in the program try to access that input data as a string.
Example
# phone is an integer
phone = int(input("Enter Your Mobile Number along with country code: "))
country_code = phone[0:2]
number = phone[2:]
print("Country Code:", country_code)
print("Phone Number:", number)
Output
Enter Your Mobile Number along with country code: 91984758447
Traceback (most recent call last):
File "main.py", line 4, in <module>
country_code = phone[0:2]
TypeError: 'int' object is not subscriptable
Analyze output
When we look at the output, it tells us that we are getting an error at
line 4
, with statement
country_code = phone[0:2]
. And the error we are receiving is
TypeError: 'int' object is not subscriptable
.
This means the
phone
is an integer object are we are trying to perform an illegal subscriptable operation
[0:2]
on it. If we look closely at the first line of the code, there we are convenient the user input into
int
using the int() function.
Solution
To solve the above problem we just need to remove the
int()
function from the input() method. Which will lead the
phone
as a string object.
Example
# phone is a string
phone =input("Enter Your Mobile Number along with country code: ")
country_code = phone[0:2]
number = phone[2:]
print("Country Code:", country_code)
print("Phone Number:", number)
Output
Enter Your Mobile Number along with country code: 91233435432
Country Code: 91
Phone Number: 233435432
Wrapping Up!
In this Python guide of typeerror we discuss one of the most common Python type errors " 'int' object is not subscriptable ". This error occurs when we perform the indexing or slicing operation on an integer object.
So, whenever you see this error on your terminal make sure that you are not performing the slicing or indexing operation on an integer object. Even though you want to slice data from an integer, you can convert the integer into a string and then perform the slicing operation.
People are also reading:
Leave a Comment on this Post