Is there any way I can check if word is in the text?


#1

is there any way I can check if word is in the text?
because if we use == , it might not catch words that is in the text split but not the same

example)
censor("hi my name is name1", "nam")
nam isn't same as any of words splits but still has to be replaced to * like "hi my ***e is ***e1" but if we use == it won't return the right answer?


Censor - hit problem with join syntax and also global function
#2

>>> a, b = "hi my name is name1", "nam"
>>> a.replace(b, "*" * len(b))
'hi my ***e is ***e1'
>>>

#3

thanks!
that is exactly what I was looking for.

a, b = "hi my name is name1", "nam"
a.replace(b, "*" * len(b))

awesome!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.


#5

Some days ago I had the misfortune of having to remove a post since it contravened solution posting guidelines. @marco1 had this post taken away from him. Days later I could dream up the code on premise of splitting on word, so the originality is probably universal. We can all take the credit for this...

>>> text = 'The rain in Spain falls mainly in the plains'
>>> word = 'in'
>>> ('*' * len(word)).join(text.split(word))
'The ra** ** Spa** falls ma**ly ** the pla**s'
>>>

Hopefully it more fully answers your question.