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
🌐
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

Discussions

String Index out of range
As an ignorant, I checked more times my code but I can’t find any error in the way I indexed… So, what’s wrong with this? def elenco_completo(): def chiudi_elenco(): frm_infospiti.destroy() frm_infospiti = Frame(root) frm_infospiti.pack() entry = Entry(frm_infospiti, width=25, borderwidth=4) ... More on discuss.python.org
🌐 discuss.python.org
10
0
February 25, 2023
Python: String index out of range - Stack Overflow
I have had a look at a lot of previous questions put up, but still couldn't find anything that'll help me here. Here's a code I wrote to Reverse a sentence. I could have used split() function, but I More on stackoverflow.com
🌐 stackoverflow.com
Erreur "string index out of range", besoin d'aide svp.
🌐 r/learnpython
Python string index out of range
When the loop is at 4th iteration, start becomes 3 and s[start+1] will throw index out of range error because start+1 is 4 and s has a length of 4 which in turn means any positive index number greater than 3 will throw index out of range error. More on reddit.com
🌐 r/learnpython
5
1
November 13, 2022
🌐
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 - IndexError in String can be handled by ensuring character accessed are within the range of given string, by using len() method.
🌐
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
Traceback (most recent call last): ... 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....
Published   April 24, 2023
🌐
freeCodeCamp
freecodecamp.org › news › list-index-out-of-range-python-error-solved
List Index Out of Range – Python Error [Solved]
August 3, 2022 - The easy fix is to always use an index that exists in a list when trying to access items in the list. Loops work with conditions. So, until a certain condition is met, they'll keep running.
🌐
Python.org
discuss.python.org › python help
String Index out of range - Python Help - Discussions on Python.org
February 25, 2023 - As an ignorant, I checked more times my code but I can’t find any error in the way I indexed… So, what’s wrong with this? def elenco_completo(): def chiudi_elenco(): frm_infospiti.destroy() frm_infosp…
Find elsewhere
🌐
LearnPython.com
learnpython.com › blog › list-index-out-of-range
How to Fix the “List index out of range” Error in Python | LearnPython.com
June 26, 2023 - You should now know what the index out of range error in Python means, why it pops up, and how to prevent it in your Python programs. A useful way to debug this error and understand how your programs are running is simply to print the index and compare it to the length of your list. This error could also occur when iterating over other data structures, such as arrays, tuples, or even when iterating through a string...
🌐
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 - The “TypeError: string index out of range” error is raised when you try to access an item at an index position that does not exist. You solve this error by making sure that your code treats strings as if they are indexed from the position ...
🌐
Codecademy
codecademy.com › forum_questions › 5130e03de984fc7bda00caa5
IndexError: string index out of range problem | Codecademy
You also have to indent your code correctly. Python is not using {} to deliminate a condition.
🌐
OnlineGDB
question.onlinegdb.com › 4846 › how-to-fix-string-index-out-of-range-in-python
how to fix "string index out of range " in python?? - OnlineGDB Q&A
October 27, 2019 - Your "z" variable is wrong. You should make so it starts from 1 (to compare next letter in the strings; you already compared letters in i and j, so now you should compare i+1 and j+1) and ends on last letter of shorter string · should measure how many characters left to compare in each string ...
🌐
Rollbar
rollbar.com › home › how to fix python’s “list index out of range” error in for loops
Fix Python List Index Out of Range Error | Rollbar
You see, in Python, when you attempt to access an element using an index that lies outside the valid index range of the list, you're essentially telling the program to fetch something that isn't there, resulting in this common error.
Published   March 25, 2025
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.6 documentation
Raised when a sequence subscript is out of range. (Slice indices are silently truncated to fall in the allowed range; if an index is not an integer, TypeError is raised.)
🌐
W3Schools
w3schools.com › python › python_lists_access.asp
Python - Access List Items
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... Note: The first item has index 0. ... You can specify a range of indexes by specifying where to start and where to end the range.
🌐
Qiita
qiita.com › python
Python エラー "string index out of range" #Python - Qiita
December 3, 2021 - エラー string index out of range 原因 リストの範囲外のインデックスを指定した。 例 l = [a,b,c] print(l[3]) Traceback (most recent call last): File "./Main.py"...
🌐
Unstop
unstop.com › home › blog › fix indexerror: list index out of range in python (+examples)
Fix Indexerror: List Index Out Of Range In Python (+Examples)
February 4, 2025 - Python’s Indexerror: list index out of range happens when accessing out-of-bounds elements. Fixes include pre-calculated lengths, controlled loops, enumerate(), range() with len(), and more. ... A Python list is a versatile data structure that allows you to store and manipulate a collection of elements, such as numbers, strings, or other objects, in an ordered manner.
🌐
Python documentation
docs.python.org › 3 › tutorial › introduction.html
3. An Informal Introduction to Python — Python 3.14.6 documentation
>>> word[42] # the word only has 6 characters Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
🌐
Quora
quora.com › How-do-you-resolve-indexerror-index-out-of-range-0-Python-3-x-development
How to resolve 'indexerror: index out of range: 0' (Python 3.x, development) - Quora
Answer (1 of 3): My best guess is that you are trying to access element[0] where element is an empty list. Take a look at the list you are accessing. E.g. type(element) and len(element) where I am pretending your list is named element. If type(element) is not list then perhaps I’m on the wrong ...
🌐
Codecademy
codecademy.com › forum_questions › 524e0750f10c60448f001750
What does the error 'list index out of range' mean? | Codecademy
Generally, list index out of range means means that you are providing an index for which a list element does not exist. Now, for this exercise they require you to create a generic function for an unknown number of strings in a list(i.e. any ...