In Python, iterable objects are indexed using numbers. When you try to access an iterable object using a string value, an error will be returned. This error will look something like “TypeError: string indices must be integers”.
In this guide, we’re going to discuss what this error means and why it is raised. We’ll walk through an example code snippet with this error and a solution to help you gain further context into how you can solve this type of error.
Let’s get started!
The Problem: typeerror: string indices must be integers
We have a TypeError on our hands. That means that we’re trying to perform an operation on a value whose type is not compatible with that operation. Let’s look at our error message:
typeerror: string indices must be integers
Like many Python error messages, this one tells us exactly what mistake we have made. This error indicates that we are trying to access a value from an iterable using a string index rather than an integer index.
Iterables, like strings and dictionaries, are indexed starting from the number 0. Consider the following list:
keyboard = ["Apex Pro", "Apple Magic Keyboard"];
This is a list of strings. To access the first item in this list, we need to reference it by its index value:
print(keyboard[0])
This will return a keyboard name: “Apex Pro”. We could not access this list item using a string. Otherwise, an error would be returned.
An Example Scenario
Let’s create a dictionary called “steel_series” which contains information on a keyboard:
steel_series = { "name": "Apex Pro", "manufacturer": "SteelSeries", "oled_display": True }
We’re going to iterate over all the values in this dictionary and print them to the console.
for k in steel_series: print("Name: " + k["name"]) print("Manufacturer: " + k["manufacturer"]) print("OLED Display: " + str(k["oled_display"]))
This code uses a for loop to iterate through every item in our “keyboard” object. Now, let’s try to run our code and see what happens:
Traceback (most recent call last): File "main.py", line 8, in <module> print("Name: " + k["name"]) TypeError: string indices must be integers
An error is present, as we expected. This error has been caused because we are trying to access values from our dictionary using string indices instead of integers.
The Solution
The problem in our code is that we’re iterating over each key in the “steel_series” dictionary.
The value of “k” is always a key in the dictionary. It’s not a record in our dictionary. Let’s try to print out “k” in our dictionary:
for k in steel_series: print(k)
Our code returns:
name manufacturer oled_display
We cannot use “k” to access values in our dictionary. k[“name”] is equal to the value of “name” inside “k”, which is “name”. This does not make sense to Python. You cannot access a value in a string using another string.
To solve this problem, we should reference our dictionary instead of “k”. We can access items in our dictionary using a string. This is because dictionary keys can be strings.
We don’t need to use a for loop to print out each value:
print("Name: " + steel_series["name"]) print("Manufacturer: " + steel_series["manufacturer"]) print("OLED Display: " + str(steel_series["oled_display"]))
Let’s run our new code:
Name: Apex Pro Manufacturer: SteelSeries OLED Display: True
Our code successfully prints out each value in our list. This is because we’re no longer trying to access our dictionary using the string value in an iterator (“k”).
Conclusion
String indices must be integers. This means that when you’re accessing an iterable object like a string, you must do it using a numerical value. If you are accessing items from a dictionary, make sure that you are accessing the dictionary itself and not a key in the dictionary.
Now you’re ready to solve the Python typeerror: string indices must be integers error like a pro!
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.