fix typeerror string indices must be integers in python

How to Fix – TypeError: string indices must be integers

The TypeError: string indices must be integers occurs when you try to access a character or a slice in a string using non-integer indices. In this tutorial, we will look at the common scenarios in which this error occurs and how we can fix it.

fix typeerror string indices must be integers in python

Understanding the TypeError: string indices must be integers error

Let’s look at some examples where we reproduce this error to better understand why this error occurs. Having clarity on why an error occurs is an essential step in understanding and ultimately fixing the error.

Here’s an example –

# create a string
s = "hello"
# using a non-integer index
s["e"]

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[3], line 4
      2 s = "hello"
      3 # using a non-integer index
----> 4 s["e"]

TypeError: string indices must be integers

In the above code, we created a string and then tried to use the string “e” as an index inside the [] notation. We get a TypeError stating that string indices must be integers.

Strings are a kind of sequence in Python. Other common types of sequences are lists, tuples, etc. Sequences are indexed using integers starting from 0. This means that the first element in a sequence has an index of 0, the second element has an index of 1, and so on. Indexes are used to access individual elements in a sequence (for example an element in a list, a character in a string, etc.). You can also use indexes to slice a sequence into smaller parts.

In the context of a string, the integer index represents the index of a specific character. For example, the index 0 is the index of the first character.

The error message TypeError: string indices must be integers is quite helpful because it tells us that only integers should be used as indices in a string. And since a different type is expected for the index, we get a TypeError.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

What if, you try to use float values as an index? For example, if you use the value 0.0 which is mathematically equal to 0, will you still get the error? Let’s find out.

# create a string
s = "hello"
# using a float value as index
s[0.0]

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[4], line 4
      2 s = "hello"
      3 # using a float value as index
----> 4 s[0.0]

TypeError: string indices must be integers

We still get the error. This shows us that only integer values are applicable as index values in Python.

Fixing the Error

Generally to fix a TypeError in Python, you have to use a value that is of the required type in the given code. In our case, it would be using an integer as an index value in a string.

For example, let’s fix the error in the above code where we’re using a float value as an index.

# create a string
s = "hello"
# using int value as index
s[int(0.0)]

Output:

'h'

Here, we first converted the float value 0.0 to an integer using the int() method and then used that integer value as the index to get the character 'h'.

Here’s a quick summary of steps that you can take to fix this error –

  1. Check the code where the error occurred and identify the line that caused the error.
  2. Make sure that you are using an integer value as an index when accessing a string. For example, if you have a string s and you want to access the first character, you should use s[0] instead of s['0'].
  3. If you are using a variable as an index, make sure that the variable is an integer. You can use the type() function to check the type of a variable.
  4. If you are still getting the error, check if you are trying to access a string using a float value as an index. In Python, you cannot use a float value as an index for a string.
  5. If none of the above steps work, try to print the value of the index variable before using it to access the string. This will help you identify any issues with the value of the index variable.

Once you have identified and fixed the issue, you should be able to run your code without getting the “TypeError: string indices must be integers” error.

Conclusion

The TypeError: string indices must be integers error occurs when you try to access a string using a non-integer value. This error can be fixed by checking the type of the index value and converting it to an integer if necessary.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top