Last modified: Apr 23, 2026 By Alexander Williams

Fix TypeError: String Indices Must Be Integers

Have you seen the error TypeError: string indices must be integers? This is a common Python mistake. It happens when you try to access a character in a string using a non-integer index. Python strings are sequences. They only accept integer positions.

This error usually appears when you confuse strings with dictionaries or lists. Beginners often make this mistake while iterating or parsing data. Let's break it down step by step.

What Does This Error Mean?

In Python, a string is a sequence of characters. Each character has an index number. The first character is at index 0. To get a character, you must use an integer inside square brackets. If you use a string like "name" or a float, Python raises the TypeError.

For example, this code is wrong:

 
text = "hello"
print(text["0"])  # Wrong! "0" is a string, not an integer

TypeError: string indices must be integers

The correct version uses an integer:

 
text = "hello"
print(text[0])  # Correct: uses integer 0
# Output: h

Common Causes of This Error

1. Confusing Strings with Dictionaries

This is the most frequent cause. You might think a variable is a dictionary, but it is actually a string. Dictionaries use keys (strings) to access values. Strings only accept integers.

Example of the mistake:

 
data = "apple"  # This is a string, not a dictionary
print(data["color"])  # Error: string indices must be integers

If you intended a dictionary, write it correctly:

 
data = {"fruit": "apple", "color": "red"}  # This is a dictionary
print(data["color"])  # Output: red

2. Iterating Over a String and Treating It Like a Dictionary

When you loop over a string, each item is a single character. Beginners sometimes try to access keys inside the loop. This fails because each character is a string, not a dictionary.

Bad example:

 
my_string = "cat"
for char in my_string:
    print(char["name"])  # Error! char is a string, not a dict

Correct way:

 
my_string = "cat"
for char in my_string:
    print(char)  # Output: c, a, t

3. Using a Variable That Holds a String Instead of a List or Dict

Sometimes you load data from a file or API. You expect a list of dictionaries, but you get a string. This often happens with JSON data that was not parsed.

Example:

 
import json
json_string = '{"name": "Alice"}'  # This is a string, not a dict
print(json_string["name"])  # Error! String indices must be integers

Fix by parsing the JSON first:

 
import json
json_string = '{"name": "Alice"}'
data = json.loads(json_string)  # Convert string to dictionary
print(data["name"])  # Output: Alice

How to Fix It

Follow these steps when you see this error:

Step 1: Check the variable type. Use the type() function. If it returns <class 'str'>, you are working with a string. You cannot use string keys on it.

Step 2: Verify your data source. If you parse a file or API response, make sure you converted it to a dictionary or list. For example, use json.loads() for JSON strings.

Step 3: Use integer indices for strings. If you need a character, use an integer like text[2]. If you need to find a substring, use methods like find() or slicing.

Step 4: Debug with print statements. Print the variable and its type to confirm what you are working with.

Here is a debug example:

 
data = "hello"
print(type(data))  # 
# Now you know it's a string. Use integer index.
print(data[0])  # Output: h

Real-World Example: Parsing a CSV Line

Imagine you read a CSV file. Each line is a string. If you try to access columns by name, you get this error.

Wrong code:

 
line = "Alice,30,Engineer"
print(line["name"])  # Error! line is a string

Correct code: split the string first, then use integer indices or convert to a dictionary.

 
line = "Alice,30,Engineer"
parts = line.split(",")  # Now parts is a list
print(parts[0])  # Output: Alice

If you want named access, create a dictionary:

 
line = "Alice,30,Engineer"
keys = ["name", "age", "job"]
values = line.split(",")
person = dict(zip(keys, values))
print(person["name"])  # Output: Alice

Preventing the Error

Always validate your data types. Use isinstance() to check before indexing. This is especially useful when working with mixed data from external sources.

Example:

 
def get_value(data, key):
    if isinstance(data, dict):
        return data.get(key, "Key not found")
    elif isinstance(data, str):
        return "Data is a string, not a dictionary"
    else:
        return "Unsupported type"

my_data = "hello"
print(get_value(my_data, "name"))  # Output: Data is a string, not a dictionary

Related Concept: Python TypeError: Causes and Fixes

This error is part of a larger family of type errors. If you want to understand other common type errors in Python, check out our guide on Python TypeError: Causes and Fixes. It covers similar issues like TypeError: list indices must be integers or slices and TypeError: 'int' object is not subscriptable.

Conclusion

The TypeError: string indices must be integers is easy to fix once you understand the cause. Always remember: strings use integer indices, dictionaries use string keys. Use type() to check your variables. Parse JSON and CSV data correctly. With these tips, you will avoid this error in your Python projects.

Practice writing clean code. Test your data types early. This will save you time and frustration. Happy coding!