ItsMyCode |
In Python, the iterable objects are indexed using numbers. If you try to access the iterable objects using string, you will get typeerror: string indices must be integers.
Python TypeError: string indices must be integers
All the iterable objects such as lists, dictionaries, strings are indexed using the number, and the index starts from 0. Let’s look at the following example of a list and access the element using the number index.
mylist=["Joe","Rachel","Chandler","Monica"]
print("The second element in the list is ", mylist[1])
Output
The second element in the list is Rachel
Example 1
series ={
"name":"Friends",
"seasons":10,
"releasedate": 2000
}
for i in series:
print("Name of series ", i["name"])
print("total no of seasons in series ", i["seasons"])
print("Release date is ", i["releasedate"])
Example 2
text= "Hello World"
print(text['hello'])
Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\Python Tutorial.py", line 8, in <module>
print("Name of series ", i["name"])
TypeError: string indices must be integers
If you look at the above examples, we have declared a dictionary object in the first one. We are iterating the series object using for loop and trying to print the value of the dictionary using the indices instead of an integer.
In another example, we have a string, and accessing the character of a string needs to be done using an integer, but instead, we use a string here, which again will lead to typeerror: string indices must be integers.
*Solution – * string indices must be integers
The major problem in our code was iterating the dictionary keys and indexing using string. We cannot use the index of key to access the values. Instead, we can print the keys as shown below.
series ={
"name":"Friends",
"seasons":10,
"releasedate": 2000
}
for i in series:
print(i)
Output
name
seasons
releasedate
If you want to print the values in the dictionary, do not use any loop or iterate using the loop. Instead, access the dictionary using the key to print its value, as shown below.
series ={
"name":"Friends",
"seasons":10,
"releasedate": 2000
}
print("Name of series ", series["name"])
print("total no of seasons in series ", series["seasons"])
print("Release date is ", series["releasedate"])
Name of series Friends
total no of seasons in series 10
Release date is 2000
The post TypeError: string indices must be integers appeared first on ItsMyCode.
Top comments (1)
All the characters of a string have a unique index . This index specifies the position of each character of the string. TypeError: string indices must be integers means an attempt to access a location within a string using an index that is not an integer.
For example, str[hello"] and str[2.1] as indexes. As these are not integers, a TypeError exception is raised. This means that when you’re accessing an iterable object like a string or float value, you must do it using an integer value .
Python supports slice notation for any sequential data type like lists, strings , tuples, bytes, bytearrays, and ranges. When working with strings and slice notation, it can happen that a TypeError: string indices must be integers is raised, pointing out that the indices must be integers, even if they obviously are.
net-informations.com/python/err/in...