close

[Solved] Python error: “IndexError: string index out of range”

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python error: “IndexError: string index out of range” in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python error: “IndexError: string index out of range” Error Occurs?

Today I get the following error Python error: “IndexError: string index out of range” in python.

How To Solve Python error: “IndexError: string index out of range” Error ?

  1. How To Solve Python error: “IndexError: string index out of range” Error ?

    To Solve Python error: “IndexError: string index out of range” Error You are iterating over one string (word), but then using the index into that to look up a character in so_far. There is no guarantee that these two strings have the same length.

  2. Python error: “IndexError: string index out of range”

    To Solve Python error: “IndexError: string index out of range” Error You are iterating over one string (word), but then using the index into that to look up a character in so_far. There is no guarantee that these two strings have the same length.

Solution 1

It looks like you indented so_far = new too much. Try this:

if guess in word:
    print("\nYes!", guess, "is in the word!")
    # Create a new variable (so_far) to contain the guess
    new = ""
    i = 0
    for i in range(len(word)):
        if guess == word[i]:
            new += guess
        else:
            new += so_far[i]
    so_far = new # unindented this

Solution 2

You are iterating over one string (word), but then using the index into that to look up a character in so_far. There is no guarantee that these two strings have the same length.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read