How to Fix “TypeError: String Indices Must Be Integers” in Python?

How to fix: "TypeError: String Indices Must Be Integers" in Python

Are you facing the “TypeError: string indices that must be integers” error in Python while accessing the values of strings, tuples, and lists? 

TypeError is familiar exception developers face in Python when the data type of objects is used in an invalid manner. And the error string indices must be integers in Python occurs when you are accessing the values of iterables like strings, tuples, and lists in an invalid manner.

This article will focus on the “TypeError: string indices that must be integers” error in Python and how to fix it. But before going into the details of the error, let’s first discuss what a TypeError is in Python. 

 

 

What is a TypeError in Python?

TypeError is one of the most common types of exception in Python that occurs when you are using the object in an invalid which isn’t supported by Python syntax. Instead, it represents an error when the operation cannot be performed in Python.

There could be different reasons behind the cause of TypeError in Python; let’s look at a few scenarios that cause TypeError.

 

Code examples that cause TypeError in Python

Code Example 1

print(3/"x")

 

Output

TypeError: unsupported operand type(s) for /: 'int' and 'str'

 

Code Example 2

print('2'+2)

 

Output

TypeError: can only concatenate str (not "int") to str

 

Code Example 3

lst = [1, 2]

tpl = (4, 5)    

print(tpl+lst)

 

Output

TypeError: can only concatenate tuple (not "list") to tuple

 

Code Example 4

myInt = 100

myInt()

 

Output

TypeError: 'int' object is not callable

 

Code Example 5

string = "GuidingCode.com"

print(string['G'])

 

Output

TypeError: string indices must be integers

 

Alright! Now that we have understood what a TypeError is in Python, let’s move towards understanding the “TypeError: string indices must be integers” error more specifically.

 

 

What is the “TypeError: String Indices Must be Integers” in Python?

The error happens when you access an iterable with an invalid index. An index is a location of a value in an iterable, and its data type should be an integer. 

So long story short, to access values of the strings, lists, or tuples, you’ll need an index, but when you try to use an invalid index, you’ll get the string indices must be integers error.

Let’s see a few examples that cause the TypeError string indices must be integers.

 

Code

# create a list of coding sites

lst = ["GuidingCode","StackOverFlow","Delftstack","Quora"]



# access the first site and print it

print(lst['GuidingCode'])

 

Output

TypeError: list indices must be integers or slices, not str

 

Code

# create a tuple of countries

tpl = ["Malaysia", "Pakistan","USA","UK","Turkey"]


# access the first item of the tpl

print(tpl['1'])

 

Output

TypeError: list indices must be integers or slices, not str

 

Code

# create a string

string = "GuidingCode.com"

# access the first value of the string

print(string['1'])

 

Output

TypeError: string indices must be integers

 

As you can see, all the above are different scenarios, but each of these has TypeError string indices that must be integers. And the reason is that if you can see where you are using an invalid index for each iterable to access data, the index should be an integer.

 

 

How to Fix the “TypeError: String Indices Must be Integers” in Python?

So far, we have discussed TypeError and the different scenarios that cause it. So now it’s time to fix the error in Python. 

We understood that the index caused the error because the index as a string isn’t supported in Python. Let’s use a valid index to access the values from an iterable 

The index always starts with 0, and to access the first element of an iterable, you have to use the index as 0 instead of 1.

 

Code

# create a list of coding sites

lst = ["GuidingCode","StackOverFlow","Delftstack","Quora"]




# access the first site and print it

print(lst[0])

 

Output

GuidingCode

 

Code

# create a string

site = "GuidingCode.com"

 

# access the 6th element of the string

print(site[5])

 

Output

n

 

You can also use the index as a sublist, but again the index range should be integer values. For example, let’s print the first 11 values of the string site. 

 

Code

# create a string

site = "GuidingCode.com"



# access the 6th element of the string

print(site[0:11])

 

Output

GuidingCode

 

This code says to the Python compiler, “Hey! Compiler, please print the values from locations 0 to 11 from the string site“. Isn’t this cool? 😎

 

 

Conclusion

To summarize the article on how to fix the “TypeError: string indices that must be integers” error in Python, we have discussed what a TypeError is and the different scenarios that cause the TypeError in Python. In addition, we’ve talked specifically about what the error is and how to fix this, along with practical examples and implementations.

Let’s have a quick recap of the topics we’ve discussed in this article,

  1. What is a TypeError in Python?
  2. The different scenarios that cause a TypeError in Python.
  3. What is the “TypeError: String Indices Must be Integers” error?
  4. How to fix this error?
  5. How to access a substring from a string?

Sharing is caring 😉; comment below what are the other scenarios that cause a TypeError in Python.

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share