Question
In Python, what happens if we try to access an invalid index of a string?
Answer
If we try to access an index of a string that’s not a valid index, then the IndexError
will be thrown. This will also apply when using negative indices, as it will try to access an index starting from the final index moving left.
Example
string = "Hello"
# These will both give an IndexError
print(string[5])
print(string[-6])