我有一个字符串,我想替换该字符串某些索引处的字符。但我只知道如何替换一个字符,如果我得到一个索引使用:
word = word[:pos] + 'X' + word[pos + 1:]
pos 在这种情况下是索引。但是当我现在有一个多个索引的列表时(所以 pos 现在是一个列表),它不起作用,因为切片索引必须是整数。
这里有一些更多的代码来提供 mor 上下文:
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)
HUX布斯
天涯尽头无女友
相关分类