I have a string and I want to replace characters at certain indices of that string. But I only know how to replace a character if I got one index using:
word = word[:pos] + 'X' + word[pos + 1:]
pos in this case is the index. But when I now have a list of multiple indices (so pos is a list now), it does not work, because slice indices must be integers.
Here is some more code to give mor context:
string = 'HELLO WORLD'
secretword = ''.join('_' for c in string)
while True:
userinput = input("Give me a letter\n").upper()
if len(userinput) == 1:
if userinput in string:
pos = [i for i in range(len(string)) if string[i] == userinput]
secretword = secretword[:pos] + userinput + secretword[pos + 1:] #this does not work
print(secretword)
replacefunction?