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
Answer from Rob Wouters on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-index-out-of-range-how-to-fix-indexerror
Python string index out of range - How to fix IndexError - GeeksforGeeks
July 23, 2025 - For positive index, the range must be within [0, len(string) - 1] and for negative index, rage should be [-len(string), -1]. Combined validation [for both +ve and -ve indexing] can be done with -len(string) <= index < len(string).
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ how to fix indexerror: string index out of range in python
How to Fix IndexError: string index out of range in Python | Rollbar
The Python IndexError: string index out of range can be fixed by making sure any characters accessed in a string are within the range of the string. This can be done by checking the length of the string before accessing an index.
Published ย  April 24, 2023
Discussions

Python error: "IndexError: string index out of range" - Stack Overflow
I'm currently learning python from a book called 'Python for the absolute beginner (third edition)'. There is an exercise in the book which outlines code for a hangman game. I followed along with t... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How To Fix String Index Out of Range in Python - Stack Overflow
I'm currently learning python. There is an exercise in the book which outlines code for decoding. I followed along with this code however I keep getting back an error in the middle of the program. ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to fix String index out of range: -1 - Stack Overflow
I have been working on a generating password method that will change every "S" to $. Note I take the phrase in from another class and it will always be greater than 8 characters String key; ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 24, 2019
How to fix string index out of range errror
I'm trying to display details in a QLabel but when I try I get the following error, string index out of range. Below is the code that generated the error, I ... More on forum.qt.io
๐ŸŒ forum.qt.io
2
0
August 17, 2022
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ string index out of range error
r/learnpython on Reddit: String index out of range error
December 21, 2023 -

I'm writing a program that needs to iterate through a string and print the first letter of every word. I have the program working but I can't submit it because of a "String index out of range" error.

An example of what needs to happen is this:

Please type in a sentence: Humpty Dumpty sat on a wall

Output:

H

D

s

o

a

w

Here's my code:

sent = input("Please type in a sentence: ")
index = 0
sent_length = len(sent)
space = " "
print(sent[index])
while index <= sent_length:
    index += 1
    if sent[index] == space:
        print(sent[index + 1])

I'm getting the correct output but it says I have a "string index out of range" error on line 8. I can't figure out what's wrong with it

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 65096354 โ€บ how-to-fix-string-index-out-of-range-in-python
How To Fix String Index Out of Range in Python - Stack Overflow
def decode(string_for_decoding: str): result = "" for i in range(0, len(string_for_decoding)): if len(string_for_decoding) > i + 1 and string_for_decoding[i + 1].isdigit(): result += string_for_decoding[i] * int(string_for_decoding[i + 1]) elif string_for_decoding.isalpha(): result += string_for_decoding[i] return result print(decode(input("Enter a string to decode: "))) ... You are going from 0 to len(string) and inside for loop you are trying to access index: i+1 THis is the root cause!
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python typeerror: string index out of range solution
Python TypeError: string index out of range Solution | Career Karma
December 1, 2023 - You solve this error by making sure that your code treats strings as if they are indexed from the position 0. Now youโ€™re ready to solve this common Python error like a professional coder!
Find elsewhere
๐ŸŒ
Python Guides
pythonguides.com โ€บ indexerror-string-index-out-of-range-python
Python String Index Out of Range
March 16, 2026 - However, you can still get an IndexError if your negative index is larger than the string length. # US Currency Example currency = "USD" try: # Trying to get the 4th character from the back print(currency[-4]) except IndexError: print("Negative index is also out of range!")
๐ŸŒ
Qt Forum
forum.qt.io โ€บ home โ€บ qt development โ€บ qt for python โ€บ how to fix string index out of range errror
How to fix string index out of range errror | Qt Forum
August 17, 2022 - I'm trying to display details in a QLabel but when I try I get the following error, string index out of range. Below is the code that generated the error, I ...
๐ŸŒ
Quora
quora.com โ€บ Why-am-I-getting-String-Index-out-of-range
Why am I getting String Index out of range? - Quora
A StringIndexOutOfBoundsException (commonly phrased โ€œString index out of rangeโ€) happens when code tries to access a character position that does not exist in the string. Common patterns, reasons, and fixes:
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 56415342 โ€บ how-to-fix-string-index-out-of-range-error
python - How to fix 'String index out of range' error - Stack Overflow
You can iterate over the string, keeping a running counter and create your string as you go ยท s = 'aaaaggggtt' res = '' counter = 1 #Iterate over the string for idx in range(len(s)-1): #If the character changes if s[idx] != s[idx+1]: #Append ...
๐ŸŒ
STechies
stechies.com โ€บ indexerror-string-index-out-range
IndexError: string index out of range
This is because all of the string indices mentioned within the print statement are within the range of the string Stechies. ... # Declaring String str1 = 'STECHIES' i=0 # while loop less then and equal to list "str1" length.
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ resolve-java-stringindexoutofboundsexception
How to Resolve a java.lang.StringIndexOutOfBoundsException: String Index Out of Range Error - CodingTechRoom
Solution: Use str.length() to ensure you won't exceed the index. Mistake: Neglecting to handle exceptions when dealing with user input that affects string manipulation. Solution: Implement proper exception handling with try-catch blocks.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 39841129 โ€บ string-index-out-of-range-error-how-to-fix
java - String Index Out of Range Error: How to Fix? - Stack Overflow
String word = "Hello"; String empty = ""; StringBuffer sb1 = new StringBuffer(word); StringBuffer sb2 = new StringBuffer(empty); Random randomChar = new Random(); while (word.length() != 0 && sb1.length() != 0) { int charIndex = randomChar.nextInt(word.length()); if (sb1.length() > charIndex) { char character = sb1.charAt(charIndex); sb2.append(character); sb1.deleteCharAt(charIndex); } } System.out.println(word.length()); System.out.println(word); System.out.println(sb2); ... Just providing replacement code without explaining what you fixed is a poor answer.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 372047 โ€บ java โ€บ string-index-range
string index out of range (Java in General forum at Coderanch)
************************************************************ String outstring = "123"; String repid; String incno; int MessageLength = MQ_Message.getMessageLength( ); outString = MQ_Message.readString(MessageLength); repid = outString.substring(83,13); incno = outString.substring(96,9); ************************************************** Thanks in advance Ann ... repid = outString.substring(83,13); incno = outString.substring(96,9); The two arguments to substring are the first (inclusive) and last (exclusive) character to include; it looks like you're assuming the second argument is a count, which it's not.
๐ŸŒ
Rrtutors
rrtutors.com โ€บ tutorials โ€บ index-error-string-index-out-of-range
Index error: string index out of range (Resolved)
December 30, 2020 - Indexes in Python programming begin from 0. It means that the index for any string will up to length-1. This creates your amounts [8] fall short as the requested index is larger compared to the length of this string. ... In python string indexing starts from 0 that means the index for any string will be upto length -1. Even the string index out of range error must complete having a common beginner issue when obtaining parts of the string with its index.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 26618182 โ€บ how-to-fix-indexerror-string-index-out-of-range
python - How to fix IndexError: string index out of range - Stack Overflow
If you have a string of length 10, the valid indexes are 0 to 9, so 10 is out of range. The quickest fix would be to change your initializations outside the while loop to.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ string index out of range [java]
r/learnprogramming on Reddit: String index out of range [Java]
March 28, 2019 -

Hello!

I can not figure out why it is coming up as out string out of range. I would love a second pair of eyes.

String numbers = scanObj.nextLine();

// make sure to only use numbers in case user input letters.
int i=0;
numbers.length();
char x =numbers.charAt(i);
if ((x >= 48 && x<=57))
// convert in to int
  for (i = numbers.length(); i !=0; --i)
    {
    int numbers1 = numbers.charAt(i)*10^i;
    {
System.out.println(numbers1);
    }
    }