Home Python Python TypeError: String Index Out Of Range Solution

Python TypeError: String Index Out Of Range Solution

0
Python TypeError: String Index Out Of Range Solution
String Index Out Of Range Solution

A string is an array of characters and part of just about any programming language. In Python, the string index out of range means that which character you are finding that is not exit in your give list.

On this page, we are here to help out you to avoid this error. First of all, we want to show you one example that will give you this error.

let’s get started

String index out of range Solution

Example


English = "A", "B","C", "D", "E"
print(English[5])

Output:

Traceback (most recent call last):
     File "E:\py\test.py", line 53, in <module>
           print(English_[5])
IndexError: tuple index out of range

Above you can see the string index out of range came, why it came. Because we are taking something that does not exist.

The len() function

Every single programmer needs to know that The index starts from 0 and ends with the number of characters in the string. So, First need to know the len() of your variable to know the len() follow the below code. Get Length Of List In Python.


English = "A", "B","C", "D", "E"
print(len(English))

For example:

You have a list of English characters such as make a  variable = [“A”, “B”, “C” “D”, “E”], you want to get C how will you get it? just print(English[2]) for more understanding follow the below code.

#          0    1   2    3    4
English = "A", "B","C", "D", "E"
print((English)[2])

Read More: Python IndexError: list assignment index out of range

LEAVE A REPLY

Please enter your comment!
Please enter your name here