How to Fix the “TypeError: string indices must be integers Error” in Python?

In this tutorial, I will explain how to fix the “TypeError: string indices must be integers” error in Python. I encountered this error while on a project for one of my clients. I will explain the causes of TypeError and how to fix it with suitable examples and screenshots of executed example code.

String Indices in Python

In Python, strings are ordered sequences of characters. Each character in a string has a corresponding index number, starting from 0 for the first character. For example, let’s say we have a string variable named name with the value “John”:

name = "John"

The indices for each character in the string would be:

  • J: index 0
  • o: index 1
  • h: index 2
  • n: index 3

To access a specific character in a string, you use square brackets [] and provide the index number. For example, to access the first character “J” in the name string, you would use name[0].

Read How to Remove Characters from a String in Python?

Causes of the “TypeError: string indices must be integers” Error in Python

The “TypeError: string indices must be integers” error in Python occurs when you try to access a character in a string using a string index instead of an integer index. This often happens when you accidentally use a string variable or a string literal as the index.

Here’s an example that demonstrates the error:

state = "California"
print(state["C"])  

Output:

TypeError: string indices must be integers

In the screenshot below you can see the executed example code with the output.

Fix the TypeError string indices must be integers Error in Python

In this example, we are trying to access the character “C” in the state string using the string index "C". However, Python expects an integer index, not a string index, which leads to the “TypeError: string indices must be integers” error.

Check out How to Remove HTML Tags from a String in Python?

How to Fix the “TypeError: string indices must be integers” Error in Python

To fix the “TypeError: string indices must be integers” error, you need to make sure you are using integer indices to access characters in a string. Here are a few ways to resolve the error:

1. Use integer indices:

If you know the index number of the character you want to access, use an integer instead of a string. For example:

state = "California"
print(state[0]) 

Output:

C

In the screenshot below you can see the executed example code with the output.

How to Fix the TypeError string indices must be integers Error in Python

Read How to Convert a Comma-Separated String to a List in Python?

2. Use the index() method:

If you have a string index and want to find its corresponding integer index, you can use the index() method. For example:

state = "California"
index = state.index("C")
print(state[index]) 

Output:

C

In the screenshot below you can see the executed example code with the output.

Fix the TypeError string indices must be integers Error in Python example

The index() method returns the integer index of the first occurrence of the specified substring.

Check out How to Split String by Whitespace in Python?

3. Iterate over the string:

If you need to access each character in a string, you can use a loop to iterate over the string. For example:

name = "John"
for char in name:
    print(char)

This way, you don’t need to use indices explicitly, and you can work with each character directly.

Check out How to Repeat a String Multiple Times in Python?

Example

Let’s consider a real-world scenario where you might encounter the “TypeError: string indices must be integers” error. Suppose you have a list of customer names, and you want to extract the first letter of each name to create acronyms. Here’s how you can do it correctly:

customers = ["John Doe", "Emma Smith", "Michael Johnson"]

acronyms = []
for customer in customers:
    first_letter = customer[0]
    acronyms.append(first_letter)

print(acronyms)  # Output: ["J", "E", "M"]

In this example, we iterate over each customer name in the customers list. We access the first character of each name using the integer index 0 and append it to the acronyms list. This approach avoids the “TypeError: string indices must be integers” error by using integer indices correctly.

Read How to Convert Datetime to String in Python?

Conclusion

In this tutorial, I have explained how to fix the “TypeError: string indices must be integers” error in Python. I have discussed causes of TypeError and we also discussed how to fix it. I also showed a real-time example to illustrate the TypeError.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.