+ 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?

27th Mar 2021, 8:45 AM
Prajakta Joshi
Prajakta Joshi - avatar
3 RĂ©ponses
+ 3
You want to modify only word[5]? In the case: word = word[:5] + "a" + word[6:]
27th Mar 2021, 9:01 AM
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż - avatar
+ 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))
27th Mar 2021, 9:25 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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".
27th Mar 2021, 8:59 AM
Ipang