How to replace a specific index position in a list

Hello! So, just as the title says, how do I replace a specific index position? If you look at the bottom where it says blankword.replace(blankword[i],guess), that is what I’m having trouble with. I think I know why it doesn’t work, but I don’t know what else to do. All characters in the list (except for the spaces) are “-”, so I can’t tell it to replace a specific character, I need to replace a specific position with a letter, with the value of “guess”.

Thanks,

p.s. i know my code is a mess. i basically just started it.

#hang
#print("+----+\n|\n|\n|\n|")
#man
#print("\O/\n |\n/ \\")

wordlist = []
blankword = []

print("\n")

print("+----+\n|   \O/\n|    |\n|   / \\\n|\n\n")

word = ("hello world")
word = "".join(list(word))

for i in word:
    if i != " ":
        h = i.replace(i,"-")
        wordlist.append(i)
        blankword.append(h)
    elif i == " ":
        blankword.append(i)

wordlist = "".join(wordlist)
blankword = "".join(blankword)

print(word) #remove
print(wordlist) #remove
print(blankword) #keep

guess = input()

for i in range(len(word)):
    if (word[i] == guess):
        print(i) #remove
        blankword.replace(blankword[i],guess)
        print(blankword) #remove

print(blankword)

Does this answer your question and help you?

words = ['The', 'quick', 'brown', 'fox']
words[2] = 'red'
print(words)

Some additional comments about your code:

word = ("hello world")
word = "".join(list(word))

The parentheses (...) around the string do nothing.

The second line pulls the string “hello world” apart into a list of
individual characters, and then immediately reassembles them back into a
string. So a waste of time.

Thank you very much for the reply, I put parentheses around the string for me, because it makes more sense to me, no other reason than that.

However… no, that does not answer my question. So, the the purpose of blankword.replace(blankword[i],guess) is if the user got a letter correct, then it would replace a blank point ("-") with the letter that was guessed, but only spaces that matched it. So the word is “hello world”, meaning what the user sees is “----- -----”. If they guess the letter l, the program will print “–ll- —l-”. That’s what I want to happen

I have tried doing this.

blankword[i] = guess

But results in an error.

TypeError: 'str' object does not support item assignment

Your code starts with blankword being set to an empty list, but then later it is set to a string. Strings and lists don’t behave the same way.

Strings are immutable, so there’s no syntax to “change” the letter at a position in a string. What you can do is create a new string by copying all the letters before and after the position you want to change.

>>> s = "0123456789"
>>> s = s[:4] + "-" + s[5:]
>>> s
'0123-56789'

Or build up the string one letter at a time from what you want it to be. Here’s an example of displaying only certain letters of a string.

def show_string_with_clues(hidden_text, guessed_letters):
    return "".join(x if x.lower() in guessed_letters else "-" for x in hidden_text)

my_string = "Hello World"
guessed_letters = {" "}

for guess in ["l", "x", "o", "h"]:
    print(f"I'm guessing {guess}...")
    guessed_letters.add(guess)
    print(show_string_with_clues(my_string, guessed_letters))
    print()
I'm guessing l...
--ll- ---l-

I'm guessing x...
--ll- ---l-

I'm guessing o...
--llo -o-l-

I'm guessing h...
H-llo -o-l-
1 Like