Replace'learning' in "I'm learning Python" with'******'. ( of the number of characters you want to replace)
def censor(text,word):
x = []
star = "*"*len(word)
for i in text.split():
x.append(i)
for j in x:
if j == word:
x.insert(x.index(word),star)
x.remove(j)
return " ".join(x)
print censor("I'm learning Python", "learning")
Another pattern.
def censor(text, word):
x = text.split()
for i in x:
if i == word:
x[x.index(i)]= "*"*len(word)
return " ".join(x)
print censor("I'm learning Python", "learning")
However, it is better to use enumerate () because it takes a long time to process the above.
def censor(text,word):
x = []
star = "*"*len(word)
for i in text.split():
x.append(i)
for index,j in enumerate(x):#here
if j == word:
x.insert(index,star)#here
x.remove(j)
return " ".join(x)
print censor("I'm learning Python", "learning")
def censor(text, word):
x = text.split()
for index,i in enumerate(x):#here
if i == word:
x[index]= "*"*len(word)#here
return " ".join(x)
print censor("I'm learning Python", "learning")
Thank you @usai!