DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

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

Python raises this TypeError when code uses a non-integer index on a string. Inspect the runtime value, then choose the right fix for its actual shape.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This error means Python tried to access a string with something other than an integer position or slice—often code such as data["name"] when data is actually a string. Check the value’s type at the failing line, then match your fix to its actual shape: decode JSON text, select a list element, iterate dictionary values, or use an integer index for text.

What the error means

Python strings are sequences of characters, so they support integer positions and slices, such as text[0] or text[1:4]. They do not support field-name indexing. If Python evaluates value["name"] while value is a string, it raises TypeError: string indices must be integers. The underlying cause is the mismatch between the object being indexed and the kind of index supplied—not necessarily a problem with the string’s contents. See Python’s built-in types documentation.

Find the value that has the wrong type

Use the traceback to locate the exact expression that failed. Immediately before it, print the type and representation of the object being indexed:

print(type(data))
print(repr(data))

type() shows whether the value is a string, dictionary, list, or another object. repr() shows its contents in a form that can make quotation marks, whitespace, and unexpected text easier to spot. Compare the result with what the code expects; changing an index without checking the input can merely hide the real mismatch.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the correction for the value’s actual shape

If it is JSON text, decode it first

A Python string containing JSON remains a string until decoded. For JSON text already in a variable, use json.loads():

import json

raw = '{"name": "Ada"}'
record = json.loads(raw)
print(record["name"])

For a file object, use json.load() instead:

import json

with open("record.json", encoding="utf-8") as file:
    record = json.load(file)

print(record["name"])

These functions decode JSON; the result is not guaranteed to be a dictionary. JSON can represent an object, array, string, number, Boolean, or null. Check the decoded shape before using a key. Python’s JSON documentation describes the decoder and the values it can produce.

If it is a Requests response, decode the response body

When an HTTP response body contains JSON, call response.json() to decode it. Handle HTTP status separately: parsing a response body does not establish that the request succeeded.

response = requests.get(url)
response.raise_for_status()
record = response.json()
print(record["name"])

Requests documents Response.json() and status handling in its Quickstart.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If it is a list, select or iterate its elements

A list uses integer positions, not field names. If it contains dictionaries, iterate the list so each loop variable is a dictionary:

rows = [{"name": "Ada"}, {"name": "Bo"}]

for row in rows:
    print(row["name"])

You can also select a particular element with an integer index, such as rows[0]["name"], if that is what the program needs.

If it is a dictionary, check what the loop variable contains

Iterating over a dictionary directly yields its keys. If each loop variable is expected to be a record, it may instead be a string key. Iterate over the values or key-value pairs according to the task:

users = {"u1": {"name": "Ada"}, "u2": {"name": "Bo"}}

for user in users.values():
    print(user["name"])

Use .items() when both the key and its corresponding value are needed:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
for user_id, user in users.items():
    print(user_id, user["name"])

If it really is text, use a character position

When the value is intended to remain text, access a character with an integer position or a range with a slice—for example, text[0] or text[:5]. If the goal is to find or transform text, use the relevant string operation rather than treating it as a mapping.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check for the JSON shape your code expects

If decoding JSON produces a string, the JSON value itself may be a string. For example, a JSON document can contain a quoted string rather than an object with named fields. Another possibility is that a producer encoded JSON text as a JSON string. Inspect the decoded value and the producer’s expected schema before decoding again; repeated decoding is not a general fix.

Do not use eval() to parse JSON. Use Python’s JSON decoder. Malformed JSON raises a decoding error; that is distinct from this TypeError, which occurs when code applies an unsuitable index to an object.

Tell similar errors apart

  • KeyError: The object is a mapping, but the requested key is absent.
  • JSONDecodeError: The supplied text is not valid JSON for the decoder.
  • list indices must be integers or slices, not str: The object being indexed is a list, and the code supplied a string index.

In each case, inspect the traceback expression and the runtime object to identify whether the issue is the container type, the index, or the input format.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why the error message may vary

The core behavior is covered by the Python 3.14.8 documentation. Wording can vary by Python version: the error guide notes that Python 3.11 and later may include the offending type in a message such as not 'str'. That wording change does not mean the underlying indexing rule changed.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.