Remove words starting with specific character from String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Remove words starting with specific character from String in Python #

To remove words that start with a specific character from a string:

  1. Use the str.split() method to split the string into a list of words.
  2. Use the str.startswith() method to check if each word starts with the character.
  3. Filter out words that start with the specified character.
main.py
my_str = 'One two _apple _banana three' char = '_' result = " ".join( filter( lambda word: not word.startswith(char), my_str.split() ) ) print(result) # 👉️ "One two three" # --------------------------------------------- result_2 = " ".join( word for word in my_str.split() if not word.startswith(char) ) print(result_2) # 👉️ "One two three"

The examples remove words that start with an underscore from a string.

The first step is to split the string into a list of words.

main.py
my_str = 'One two _apple _banana three' # 👇️ ['One', 'two', '_apple', '_banana', 'three'] print(my_str.split())

The str.split() method splits the string into a list of substrings using a delimiter.

If no delimiter is provided in the call to the method, the method splits the string on each whitespace character.

We used the filter() method to filter out any words from the string that start with the specified character.

main.py
my_str = 'One two _apple _banana three' char = '_' result = " ".join( filter( lambda word: not word.startswith(char), my_str.split() ) ) print(result) # 👉️ "One two three"

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

The str.startswith method returns True if the string starts with the provided prefix, otherwise the method returns False.

On each iteration, we check if the word starts with the specified character and exclude it if it does.

This approach also works if you need to remove words from a string that start with multiple specific characters.

The last step is to use the join() method to join the list of words into a string.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

If you need to check if the string starts with a specific character ignoring case, convert both strings to lowercase.

main.py
my_str = 'One two apple avocado three' char = 'A' result = " ".join( filter( lambda word: not word.lower().startswith(char.lower()), my_str.split() ) ) print(result) # 👉️ "One two three"

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

An alternative to using the filter() function is to use a generator expression with an if statement.

main.py
my_str = 'One two apple avocado three' char = 'A' result = " ".join( word for word in my_str.split(' ') if not word.startswith(char) ) print(result) # 👉️ "One two three"

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current word doesn't start with the specified character and return the result.

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.