Python Lab · Experiment 20
Remove a given word from all places in a string
PythonWeek 4JNTUH R22 · CS206ES
Aim
To write a Python program that reads a sentence and a word, and removes every occurrence of that word from the sentence, without touching other words that only contain it as a part.
Algorithm
- Read the sentence and the word to remove.
- Split the sentence into a list of words.
- Make a new list that keeps every word that is not equal to the given word, and count the words that were dropped.
- Join the kept words with single spaces to make the new sentence.
- Print how many times the word was removed and the new sentence.
Program
sentence = input("Enter a sentence: ")
target = input("Enter the word to remove: ").strip()
words = sentence.split()
kept = []
removed = 0
for w in words:
if w == target:
removed += 1
else:
kept.append(w)
new_sentence = " ".join(kept)
print(f"'{target}' was removed {removed} time(s)")
print("New sentence:", new_sentence)Output
The word appears several times
Enter a sentence: the boy said the book is there on the table
Enter the word to remove: the
'the' was removed 3 time(s)
New sentence: boy said book is there on tableThe word is not present
Enter a sentence: python is easy to learn
Enter the word to remove: java
'java' was removed 0 time(s)
New sentence: python is easy to learnRecorded by running this exact program. Typed input appears after each prompt.
How it works
The syllabus wording does not say whether a word inside a longer word (like "the" in "there") should be removed. This program removes only whole words, which is the usual meaning of "remove the word".
The quick way, sentence.replace("the", ""), removes the letters wherever they appear, so "there" becomes "re", and it leaves double spaces behind. Splitting into words avoids both problems.
The comparison is case-sensitive: "The" at the start of a sentence is not removed when the word is "the". Compare w.lower() == target.lower() to ignore case. Punctuation stuck to a word, such as "the," also stops a match.
Common mistakes
Using
sentence.replace(word, ""). It also removes the letters from inside other words and leaves extra spaces.Removing items from the list with
words.remove(w)while looping over the same list. The loop skips the item after each removal, so some occurrences are missed. Build a new list instead.words.remove(target)removes only the first occurrence. To remove every one, a loop or a list comprehension is needed.Forgetting to join the list back into a string, which prints a list with brackets and quotes.
Viva questions
Try to answer each one aloud before you open it.
What does split() do?
With no argument it breaks a string into a list of words, splitting at spaces, tabs and newlines and ignoring extra spaces.
Why not use replace to remove the word?
replaceworks on characters, not words. It would also change words that contain the target, likethere, and leave double spaces.What does list.remove(x) do?
It removes the first item equal to x from the list, and raises
ValueErrorif x is not in the list.How would you write the filtering in one line?
With a list comprehension:
kept = [w for w in words if w != target].