While working with strings in python, you might want to manipulate a string using its index. While doing so, they might come across the error “TypeError: string indices must be integers“.
In this article, we will understand the reason for this error and the steps we can take to resolve the error.
What causes the “TypeError: string indices must be integers” error in Python?
The “String indices must be integers” error occurs when you try to access a character in a string using an index that is not an integer.
For example:
string = 'hello'
print(string['h'])
The output will be the error like this:
Traceback (most recent call last):
File "/home/main.py", line 2, in <module>
print(string['h'])
TypeError: string indices must be integers
This is because, strings are indexed using integers starting from 0 for the first character, 1 for the second character, and so on. And in this example, we are trying to access the first character i.e “h”, using the index as string['h']
, which is not an integer.
Another reason that might cause this error in python is when using the slicing
method. If we use incorrect slicing syntax, it will also cause the “string indices must be integers” error in our python code.
Now, that we know the common cause of the error, let’s see the solution to resolve the error below.
Solutions to fix TypeError: string indices must be integers Error
There are two solutions to fix the “string indices must be integers” error.
Solution 1: Using numeric value as an index
In Python, there are iterable objects like strings, tuples, and lists whose items can be accessed by using index numbers.
So if we have a string “hello” and we want to access the character “e
“, we have to write like this:
string = "hello"
print(string[1]) #output: "e"
In the above example, the code string[1]
will return us the character at index 1 which is “e
“.
We can also use negative numbers as an index to access a character in a string. The negative index will start counting from the end of the string. For example:
string = "hello"
print(string[-2])
The output will be the second last letter i.e “l” from the string.
And also make sure that the index number is not wrapped inside a string quotation.
Example:
string = "hello"
print(string["2"]) # TypeError: string indices must be integers
print(string[2]) # This is the correct way
Solution 2: Using correct slicing syntax in Python
Another reason that might cause the “string indices must be integers” error is when we use the wrong syntax in slicing a string.
In slicing, we use the start
and end
index that returns all the characters between the given range.
For example:
string = "hello coder"
print(string[0:6])
Output:
"hello"
In the above example, we have given 0 (start index) and 6 (end index). This will return us all the characters within index 0 and 6 which is “hello”.
However, sometimes we write the wrong syntax while slicing a string in python, which can throw us the error. For example,
string = "hello coder"
print(string[0,6]) # TypeError: string indices must be integers
In the above example, when though we have used numerical values as indices, instead of using a colon (:
) we have used comma (,
) to separate the indices and this will cause the “string indices must be integers” error in Python.
So, make sure you use a colon (:
) while slicing a string in Python.
Conclusion:
In this article, we discussed the cause of the “TypeError: string indices must be integers” error in Python and solutions that we can use to resolve the error in our Python code.
The error commonly occurs when working with strings in Python and the two most common reasons for this error are:
- Use of string instead of index number (integer) while accessing a character in a string
- Use of wrong slicing syntax in Python.
So, we have to make sure that we only use a number as an index and we need to use the correct slicing string syntax in python to avoid this error in our code.
Related Article:
Split String and get the first element using python
Split string by whitespace in python
Replace a character in String by index in Python
Python – Check if a string is Empty or Not