In this article, we are going to explain the main reason why you will encounter the TypeError: ‘int’ object is not subscriptable error and a few different ways to fix it. Because it is essential to understand why this error occurs. So that, when you have a good understanding, you will to avoid this error in the future and write better code.
The Problem: TypeError ‘int’ object is not subscriptable
We’ll start by taking a look at this error message:
1 typeerror: 'int' object is not subscriptable
The first part of this error message, TypeError, states the type of 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.
The second part of a 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:
1 email_providers = ["Gmail", "Outlook", "ProtonMail"] 2 3 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.
Example 1. Writing a program asks a user for the date on which their next holiday starts and prints out each value on a separate line.
Let’s start by writing the main program:
1 holiday = int(input("When does your holiday begin? (mmddyyyy) ")) 2 3 month = holiday[0:2] 4 day = holiday[2:4] 5 year = holiday[4:8] 6 7 print("Month:", month) 8 print("Day:", day) 9 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.
Then, run the code:
1 Traceback (most recent call last): 2 File "main.py", line 3, in <module> 3 month = holiday[0:1] 4 TypeError: 'int' object is not subscriptable
To 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 Typeerror: ‘int’ Object Is Not Subscriptable, 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
1 holiday = input("When does your holiday begin? (mmddyyyy) ")
So, let’s try to run our code:
1 When does your holiday begin? (mmddyyyy) 02042021 2 Month: 02 3 Day: 04 4 Year: 2021
The 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.
Example 2. Typeerror ‘int’ object is not subscriptable
Let’s see another code example where this Typeerror ‘int’ object is not subscriptable error occurs. Say that I want to write a program to calculate my lucky number based on my birth date. I will calculate my lucky number by adding all digits of my date of birth, in format dd/mm/yyyy, and then adding up the digits of the total.
1 Date of birth: 04/08/1984 2 4+8+1+9+8+4=34 3 3+4=7 4 Lucky number is 7
Here is the code we initially wrote to do this calculation:
1 birthday = '04/08/1984' 2 3 sum_month_digits = (int(birthday)+int(birthday)) 4 sum_day_digits = (int(birthday)+int(birthday)) 5 sum_year_digits= (int(birthday)+int(birthday)+int(birthday)+int(birthday)) 6 7 sum_all = sum_month_digits + sum_day_digits + sum_year_digits 8 lucky_number= (int(sum_all)+int(sum_all)) 9 10 print("Lucky number is", lucky_number)
1 Traceback (most recent call last): 2 File line 8, in <module> 3 lucky_number= (int(sum_all)+int(sum_all)) 4 TypeError: 'int' object is not subscriptable
The error occurs at line #8. The reason is that the variable
sum_all is an integer, not a string. Therefore I can’t treat this variable as a container and try to access each individual number.
To fix this Typeerror, I could convert the
sum_all variable to a string and then use the square brackets to access the digits.
1 lucky_number= (int(str(sum_all))+int(str(sum_all)))
The String type is a container like type, meaning that you can access each character using the index access. See the below example:
1 var_string='Hello' 2 print(var_string)
To Sum Up With Issue Typeerror ‘int’ object is not subscriptable
To summarise, when you encounter this error is important to double-check all access by index operation, the square bracket, and make sure you are just using them against container variables, such as lists, strings, tuples, etc. So, we hope this article about Typeerror ‘int’ object is not subscriptable will help you. Comment your thought below!
Any help with website development services? Let’s ArrowHiTech help you!
If you’re considering web development services, you can explore more about us – ArrowHiTech. Above all, we are proud to be one of the most well-established outsourcing companies all around the world. In addition, with over 12 years of experience, we can provide the best software development services for your eCommerce business.
So, if you have any questions, don’t hesitate to contact us via: