AppDividend
Latest Code Tutorials

How to Solve String Indices must be Integers in Python

0

String indices must be integers. This means that when you’re obtaining the iterable object like a string, you must do it using a numerical value. If you are accessing elements from a dictionary, be certain that you are accessing the dictionary itself and not a key in the dictionary.

Python TypeError: string indices must be integers

The typeerror: string indices must be integers shows that we are attempting to access a value from an iterable using a string index rather than an integer index. Python iterable objects are indexed using numbers. When you try to obtain an iterable object using a string value, an error will be returned.

In Python, to access list elements, we can use the integers as indices.

list = ["PS5", "Xbox Series X", "Nintendo Switch"]

print(list[1])

This is a list of strings. To access the second item in this list, we need to reference it by its index value, and we get the item.

Output

Xbox Series X

We could not access this list item using a string. Otherwise, the TypeError would be returned.

Now for the list, this is fine but let’s create a dictionary and access its elements.

data = {
    "console": "PS5",
    "exclusive": "Spiderman Miles Morales",
    "genre": "Superhero"
}

Let’s iterate over all the values in this dictionary and print them.

for ele in data:
	print("Console Name: " + ele["console"])
	print("Exclusive Game: " + ele["exclusive"])
	print("The Genre is: " + str(ele["genre"]))

Output

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/app.py", line 13, in <module>
    print("Console Name: " + ele["console"])
TypeError: string indices must be integers

And we get the TypeError: string indices must be integers. The TypeError has been raised because we try to access values from our dictionary using string indices instead of integers.

Solution: string indices must be integers

The main problem in our code is that we’re iterating over each key in the “data” dictionary. The value of “ele” is always a key in the dictionary. It’s not a record in our dictionary. Let’s try to print out “ele” in our dictionary.

data = {
    "console": "PS5",
	"exclusive": "Spiderman Miles Morales",
	"genre": "Superhero"
}


for ele in data:
	print(ele)

Output

console
exclusive
genre

You can see that we get the keys to the dictionary. We cannot use “ele” to access values in our dictionary. ele[“console”] is equal to the value of “console” inside “ele”, which is “console”. 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 “ele”. 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.

data = {
    "console": "PS5",
    "exclusive": "Spiderman Miles Morales",
    "genre": "Superhero"
}

print("Console Name: " + data["console"])
print("Exclusive Game: " + data["exclusive"])
print("Game Genre: " + (data["genre"]))

Output

Console Name: PS5
Exclusive Game: Spiderman Miles Morales
Game Genre: Superhero

And we successfully printed each value in our dictionary. This is because we’re no longer trying to access our dictionary using the string value in an iterator (“ele”).

That’s it for this tutorial.

See also

Python ValueError

Python expected indented error block

Python cannot import name

 

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.