0
banword = ["/","\n",'"',"'","Badword","*","badword","badword","badword"]
okword = ["","","","","f","","Badword","badword","badword"]
for c in range (1,len(commentaries) + 1):
    azpeo = commentaries[c]
    print(azpeo)
    for c in range(len(banword)):
        azpeo.replace(banword[c],okword[c])
    commentaries[c] = azpeo

So here, i am trying to delete bad word from sentences that are contained in a dictionnary, the dictionnary look like that :

commentaries = {1 : "sentences", 2 : "sentences"}```
But nothing is changing at all and i can't understand why ?

I tried removing characters with .replace method by stocking each value in a variable and then using .replace but this does not seems to work ?

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

1

replace doesnot change your string inplace so you need to assign it to original string. You need to change

azpeo.replace(banword[c],okword[c])

To

azpeo = azpeo.replace(banword[c],okword[c])

Then it will work

Deepak Tripathi
  • 3,175
  • 1
  • 8
  • 21
0

the problem is that strings are immutable so you should do at line 7 in your example, the following:

azpeo = azpeo.replace(banword[c],okword[c])
Iulian St
  • 9
  • 1