TypeError: String Indices Must Be Integers

TypeError: string indices must be integers, not 'str'

Quick Answer

You are using a string as an index on another string, which means you likely have a string where you expected a dictionary. This often happens when iterating over a JSON string instead of parsed data.

Why This Happens

When you iterate over a string, each element is a single character. If you try to access it with a string key like item['name'], Python raises this error. The root cause is usually that you forgot to parse JSON.

The Problem

import json
response = '{"name": "Alice"}'
for item in response:
    print(item['name'])

The Fix

import json
response = '{"name": "Alice"}'
data = json.loads(response)
print(data['name'])

Step-by-Step Fix

  1. 1

    Check the data type

    Print type(your_variable) to confirm whether it is a string or a dict/list.

  2. 2

    Parse JSON if needed

    Call json.loads() to convert a JSON string to a Python dict or list.

  3. 3

    Fix iteration pattern

    If iterating over a dict, use for key, value in data.items().

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free