如何用不同位置的另一个字符替换字符串中的某些字符?
例如我有字符串:string="********u"
,列表:list=[0,2,4]
和 character = 'a'
.如何将 string[0]
、string[2]
、string[4]
替换为 a
?
最佳答案
你可以试试这个:
s = "********u"
l ={0,2,4}
final_string = ''.join(["a" if i in l else a for i, a in enumerate(s)])
输出:
'a*a*a***u'
关于python - 通过位置列表将字符串中的字符替换为另一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46992308/