Skip to content
Laviyx Learn

Try

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

  1. Read the sentence and the word to remove.
  2. Split the sentence into a list of words.
  3. Make a new list that keeps every word that is not equal to the given word, and count the words that were dropped.
  4. Join the kept words with single spaces to make the new sentence.
  5. Print how many times the word was removed and the new sentence.

Program

Python
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

Output 1
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 table

The word is not present

Output 2
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 learn

Recorded 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?

    replace works on characters, not words. It would also change words that contain the target, like there, and leave double spaces.

  • What does list.remove(x) do?

    It removes the first item equal to x from the list, and raises ValueError if 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].

Learn the concepts

All Python Programming Lab experiments →