Strip a given word from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 18, 2022

banner

Photo from Unsplash

Strip a given word from a String in Python #

Use the str.replace() method to strip a given word from a string, e.g. new_str = my_str.replace('word ', ''). The replace() method will remove the word from the string by replacing it with an empty string.

main.py
import re # ✅ strip word from string (using replace()) my_str = 'one two three four five' new_str = my_str.replace('two ', '') print(new_str) # 👉️ 'one three four five' # --------------------------------------- # ✅ strip word from string (using string slicing) word_to_remove = 'two ' idx = my_str.index(word_to_remove) new_str = my_str[:idx] + my_str[idx + len(word_to_remove):] print(new_str) # 👉️ 'one three four five' # ---------------------------------------- # ✅ strip word from string (using re.sub()) new_str = re.sub(r'two ', '', my_str) print(new_str) # 👉️ 'one three four five' # 👇️ strip word from string (case-insensitive) # new_str = re.sub(r'TWO ', '', my_str, flags=re.IGNORECASE)

The first example uses the str.replace() method to remove a word from a string.

main.py
my_str = 'one two three four five' new_str = my_str.replace('two ', '') print(new_str) # 👉️ 'one three four five'

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

We removed all occurrences of a word by replacing each occurrence with an empty string.

The example removes all occurrences of the given word from the string.

You can use string slicing if you only need to remove the first occurrence.

Strip a given word from a String using string slicing #

To strip a given word from a string:

  1. Get the index of the first character of the word in the string.
  2. Get a string slice before the word and a slice after the word.
  3. Use the addition (+) operator to concatenate the two strings.
main.py
my_str = 'one two three four five' word_to_remove = 'two ' idx = my_str.index(word_to_remove) new_str = my_str[:idx] + my_str[idx + len(word_to_remove):] print(new_str) # 👉️ 'one three four five'

We used string slicing to strip a word from a string.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[:idx] starts at the beginning of the string and goes up to but not including the first character of the word to be removed.

main.py
word_to_remove = 'two ' idx = my_str.index(word_to_remove) # 👇️ 'one ' print(repr(my_str[:idx])) # 👇️ 'three four five' print(repr(my_str[idx + len(word_to_remove):]))

The slice my_str[idx + len(word_to_remove):] starts at the character after the last character of the word to be removed.

The last step is to use the addition (+) operator to concatenate the two string slices.

Alternatively, you can use the re.sub() method.

Strip a given word from a String using string re.sub() #

Use the re.sub() method to strip a given word from a string, e.g. new_str = re.sub(r'word ', '', my_str). The re.sub() method will remove the word from the string by replacing it with an empty string.

main.py
import re my_str = 'one two three four five' new_str = re.sub(r'two ', '', my_str) print(new_str) # 👉️ 'one three four five'

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

If the pattern isn't found, the string is returned as is.

The first argument we passed to the re.sub() method is a regular expression we want to match in the string.

The second argument is the replacement for each match.

If you need to remove a word from a string, ignoring the case, specify the flags keyword argument when calling the re.sub() method.

main.py
import re my_str = 'one two three four five' new_str = re.sub(r'TWO ', '', my_str, flags=re.IGNORECASE) print(new_str) # 👉️ 'one three four five'

The example sets the flags keyword argument to mark the regular expression as case-insensitive.

In the example, the re.sub() method removes all occurrences of the string two by replacing each occurrence with an empty string, ignoring the case.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.