DEV Community

Srinivas for ItsMyCode

Posted on • Originally published at itsmycode.com on <time datetime="2021-09-12T17:08:08Z" class="date-no-year">Sep 12</time>

TypeError: string indices must be integers

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])
Enter fullscreen mode Exit fullscreen mode

Output

The second element in the list is Rachel
Enter fullscreen mode Exit fullscreen mode

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"])
Enter fullscreen mode Exit fullscreen mode

Example 2

text= "Hello World"
print(text['hello'])
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output

name
seasons
releasedate
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The post TypeError: string indices must be integers appeared first on ItsMyCode.

Discussion (0)