Introduction
In python, we have discussed many concepts and conversions. In this tutorial, we will be discussing the concept of string indices must be integers. As we all know in python, iterable objects are accessed with the help of numeric values. If we try to access the iterable object using a string value, an error will be returned. This error will look like “TypeError: string indices must be integers.”
What are string indices?
Strings are the ordered sequences of character data. String indices are used to access the individual character from the string by directly using the numeric values. The string index starts with 0, i.e., the first character of the string is at 0 indexes and so on.
String indices must be integer
In python, when we see any iterable objects, these are indexed using numbers. If we try to access the iterable object with the help of a string, an error is returned. Error shows – “TypeError: string indices must be integers.”
All the characters which are present in the strings have their unique index. The index is used to specify the position of each character of the string. But all the indexes must be integers as we cannot remember the position with the help of a character, float value, etc.
Before moving further, I want to let you know that If you are coding in Python for a while, you might get many typeerrors every other day. To solve these errors, you can read our in-depth guide of the following typeerrors:
- How to Solve TypeError: ‘int’ object is not Subscriptable
- Invalid literal for int() with base 10 | Error and Resolution
- [Solved] TypeError: Only Size-1 Arrays Can Be Converted To Python Scalars Error
- How to solve Type error: a byte-like object is required not ‘str’
Examples showing the error String indices must be integer
Here, we will be discussing all the examples which will show you the error in your program as String indices must be an integer:
1. Taking indices as string
In this example, we will be taking an input string as str. Then, we will try to access the particular index of the string with the help of the string as the index. And then, see the output. Let us look at the example for understanding the concept in detail.
#input string
str = "Pythonpool"
p = str["p"]
print(p)
Output:
Explanation:
- Firstly, we have taken an input string as str.
- str = “Pythonpool”.
- Then, we have taken a variable p in which we have passed the index value as a character in the range of the string.
- The range of index of string starts from 0 and ends at the length of string- 1.
- At last, we have printed the output.
- Hence, you can see the “TypeError: string indices must be integers” which means you cannot access the string index with the help of character.
2. Taking indices as a float value
In this example, we will take an input string as str. And then, try to access the string with the help of float value as their index. Then, we will see the output. Let us look at the example for understanding the concept in detail.
#input string
str = "Pythonpool"
p = str[1.2]
print(p)
Output:
Explanation:
- Firstly, we have taken an input string as str.
- str = “Pythonpool”.
- Then, we have taken a variable p in which we have passed the index value as the float in the range of the string.
- The range of index of string starts from 0 and ends at the length of string- 1.
- At last, we have printed the output.
- Hence, you can see the “TypeError: string indices must be integers,” which means you cannot access the string index with the help of character.
3. string indices must be integers while parsing JSON using Python
In this example, we will see that while we are parsing on JSON, string indices must be integers. Let us look at the example for understanding the concept in detail.
import json
input_string = '{"coord": {"lon": 4.49, "lat": 50.73}, "weather": [{"id": 800, "main": "Clear", "description": "clear sky", "icon": "01d"}], "base": "stations", "main": {"temp": 26.96, "feels_like": 23.13, "temp_min": 26.11, "temp_max": 27.78, "pressure": 1016, "humidity": 26}, "visibility": 10000, "clouds": {"all": 0}, "dt": 1596629272, "timezone": 7200, "id": 2793548, "name": "La Hulpe", "cod": 200}'
data = json.loads(input_string)
for i in data['weather']:
print(i['main'])
for j in data['main']:
print(j['temp'])
Output:
Solution for String indices must be integers Error
The only solution for this type of error: “String indices must be integers” is to pass the index value as the integer value. As the iterable object can only be accessed with the help of the integer value. Let us look at the example for understanding the concept in detail.
#input string
str = "Pythonpool"
p = str[5]
print("output : ",p)
Output:
Explanation:
- Firstly, we have taken an input string as str.
- Str = Pythonpool.
- Then, we have taken a variable p in which we have passed the index value as an integer in the range of the string.
- The range of index of string starts from 0 and ends at the length of string- 1.
- At last, we have printed the output.
- Hence, you can see the output as ‘n’ as the 5th character of the string is ‘n.’
Conclusion
In this tutorial, we have learned about the concept of “TypeError: string indices must be integers.” We have seen what string indices are? Also, we have seen why string indices should be integers. We have explained this type of error with the help of all the examples and given the solution code for the error. All the examples are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Hi,
Thanks for the article, very informative. However you didn’t show how you’d solve this issue for the JSON example.
How would you get “26.96” from
for i in data['main']:
print(i['temp'])
without running into this error? I’m testing it on VS Code and while the first ‘for’ works because it’s an array, the 2nd one throws the error.
Thanks in advance,
Daniel
Check your data[‘main’]. It probably contains string.