Some objects in Python are subscriptable. This means that they contain, or can contain, other objects. Integers are not a subscriptable object. They are used to store whole numbers. If you treat an integer like a subscriptable object, an error will be raised.
In this guide, we’re going to talk about the “typeerror: ‘int’ object is not subscriptable” error and why it is raised. We’ll walk through a code snippet with this problem to show how you can solve it in your code. Let’s begin!
The Problem: typeerror: ‘int’ object is not subscriptable
We’ll start by taking a look at our error message:
typeerror: 'int' object is not subscriptable
The first part of our error message, TypeError, states the type of our error. A TypeError is an error that is raised when you try to perform an operation on a value that does not support that operation. Concatenating a string and an integer, for instance, raises a TypeError.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
The second part of our message informs us of the cause.
This message is telling us that we are treating an integer, which is a whole number, like a subscriptable object. Integers are not subscriptable objects. Only objects that contain other objects, like strings, lists, tuples, and dictionaries, are subscriptable.
Let’s say you try to use indexing to access an item from a list:
email_providers = ["Gmail", "Outlook", "ProtonMail"] print(email_providers)
This code returns: ProtonMail. Lists are subscriptable which means you can use indexing to retrieve a value from a list.
You cannot use this same syntax on a non-subscriptable value, like a float or an integer.
An Example Scenario
We’re going to write a program that asks a user for the date on which their next holiday starts and prints out each value on a separate line. This program will have an error that we can solve.
Let’s start by writing our main program:
holiday = int(input("When does your holiday begin? (mmddyyyy) ")) month = holiday[0:2] day = holiday[2:4] year = holiday[4:8] print("Month:", month) print("Day:", day) print("Year:", year)
This program asks a user to insert the day on which their holiday begins using an input() statement. Then, we use slicing to retrieve the values of the month, day, and year that the user has specified. These values are stored in variables.
Next, we print out the values of these variables to the console. Each value is given a label which states the part of the date to which the value corresponds.
Let’s run our code:
Traceback (most recent call last): File "main.py", line 3, in <module> month = holiday[0:1] TypeError: 'int' object is not subscriptable
Let’s fix this error.
We have converted the value of “holiday” into an integer. This means that we cannot access it using slicing or indexing. Integers are not indexed like strings.
To solve this problem, we can remove the
int() statement from our code. The
input() statement returns a string value. We can slice this string value up using our code.
Let’s revise our
holiday = input("When does your holiday begin? (mmddyyyy) ")
Now, let’s try to run our code:
When does your holiday begin? (mmddyyyy) 02042021 Month: 02 Day: 04 Year: 2021
Our code runs successfully! We are no longer trying to slice an integer because our code does not contain an
int() statement. Instead, “holiday” is stored as a string. This string is sliced using the slicing syntax.
The “typeerror: ‘int’ object is not subscriptable” error is raised when you try to access an integer as if it were a subscriptable object, like a list or a dictionary.
To solve this problem, make sure that you do not use slicing or indexing to access values in an integer. If you need to perform an operation only available to subscriptable objects, like slicing or indexing, you should convert your integer to a string or a list first.
Now you’re ready to solve this Python TypeError like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.