r/pythonhelp • u/itachiwasright • Apr 27 '22
Python code beginner
i have a code im working on and I need help if anyone can help please,
where im defining a function:
def function(sentence, stop_words):
in which i am trying to remove all the words in the string stop_words that are present in the sentence.
I am writing this for output:
simple_sentence = stop_word_removal("This is some sentence and I know it is a long one", "and is I") print(simple_sentence)
And I need this for output:
This some sentence know it a long one
2
Upvotes
1
u/itachiwasright Apr 27 '22
def stop_word_removal(sentence, stop_words):
""" Returns new sentence with removed stop words. """
stop_words = stop_words.split()
s = sentence
for c in stop_words:
s = s.replace(c,'').replace(' ',' ')
return s
Ive tried this code above ^
but the output is:
Th some sentence know it a long one
instead of: This some sentence know it a long one