+ 2
How can i replace index of string of underscores with other word or character?
word = "_" * 10 word = word.replace(word[5], "a") O/p aaaaaaaaaa This is not working ..why?
3 Answers
+ 3
You want to modify only word[5]?
In the case:
word = word[:5] + "a" + word[6:]
+ 2
You can always just convert word to a list. Then join it back to a string when you're done modifying it.
word = "_" * 10
word = list(word)
word[5] = 'a'
print(''.join(word))
+ 1
`replace` method replaces all the given first argument by the given second argument.
Your string initially was "__________" (10 times an underscore), so when you call replace(word[5], "a") you are actually replacing any "_" in string <word> by "a".