Remove Word from String in Python

Remove word from String in Python

Remove Word from String in Python

In this tutorial, different methods are demonstrated on how to remove word from string in Python. We will study two ways to achieve this. First, we will discuss how to remove a specific word from sentences in Python. Then, we will discuss how to remove duplicate words from a string in Python.

How to Remove Word from Sentence in Python

We will first discuss methods to remove words from a string in Python.

Using the replace() function

We can use the replace() function to remove word from string in Python. This function replaces a given substring with the mentioned substring. We can replace a word with an empty character to remove it.

For example,

Output:

remove from this

We can also specify how many occurrences of a word we want to replace in the function. For this, we can use the count parameter. By default, all occurrences are replaced.

Using the re.sub() function

The regular expressions can identify parts of a string using a pattern. The re.sub() function replaces a given substring that matches the regular expression pattern with some desired string.

We can identify specific words using regular expressions and substitute them with an empty string to remove them.

See the code below.

Output:

remove from this

In the above example, the re.compile() function compiles a pattern that identifies the substring word.

Using the startswith() function

This method can remove word from the start of the sentence. The startswith() function returns True or False, based on whether the string starts with a given value or not.

In this method, if the function returns True, we will slice the string till the length of the word to be removed.

See the code below.

Output:

remove from this

Using the removeprefix() function

This is similar to the previous method. It will only remove words from the start of the sentence if they exist. This function only exists in Python 3.9 and above.

For example,

Output:

remove from this

Using the endswith() function

This method can remove a word from the end of a sentence. The endswith() function returns True or False, based on whether the string ends with a given value or not.

Here also, we will slice the string if the function returns True.

See the code below.

Output:

remove from this

Using the removesuffix() function

This method is similar to the previous one and can eliminate a word from the end of the string. It is only available in Python 3.9 and above.

For example,

Output:

remove from this

How to Remove Duplicate Words from String in Python

We will now discuss how to remove duplicate words from string in Python. A common operation in these methods involves splitting a string into a list of words. For this, we use the split() function as shown in the sample codes.

Using the set() function

A set is an unordered collection of elements. It contains only unique elements. We can use it to store a collection of unique words from a string.

We can then use a for loop to compare each word and check whether it belongs in the set object or not. If the object is not present, it is appended to the final string.

We implement this logic in the code below.

Output:

remove word from this

In the above example, we can observe that we have successfully removed any duplicate words from the string a1.

Using the set() and join() functions

This method uses a similar approach to the previous method. We will proceed by splitting a string into a list of words. We will then pass this list to the set() function and automatically remove any duplicate words.

After this, we will convert the words stored in the set object back to a string. For this, we will use the join() function. With the join() function, we can combine the elements of an iterable in a string by providing the separator character for the elements.

Let us now use both these functions to remove duplicate words from a string in Python.

See the code below.

Output:

remove word from this

In the above example, we use the sorted() function to maintain the order of the words in the string. We sort it by their index in the list l.

Using the join() and a user-defined function

This method also follows a similar approach to the previous one. We will start by splitting the string into a list of words. In this method, instead of using the sets to remove any duplicate, we will create a function that will eliminate duplicate words from the list.

For example,

Output:

remove word from this

In the above example, the lst_unique() function ensures that every element of the list is unique.

Using the collections.OrderedDict class

The collections.OrderedDict class creates a dictionary by arranging the order of the elements. We store the elements as keys and combine them using the join() function.

For example,

Output:

remove word from this

Using the numpy.duplicate() function

The numpy.duplicate() function creates arrays from existing arrays, lists by eliminating the duplicate elements. We can use the list of words to create such an array of unique elements. After this, we will combine the elements using the join() function as done in the previous methods.

The downside of this method is that it sorts the element, so the original order of the string is lost.

See the code below.

Output:

from remove this word

Using the regular expressions

We can use regular expressions to detect sub-strings based on regular expression patterns. We can use regular expressions to remove consecutive duplicate words using some pattern.

We will use the re.sub() function to substitute the words that will match this pattern with the first occurrence of the word.

See the code below.

Output:

remove word from this

Conclusion

This article demonstrated how to remove word from String in Python. Let’s wrap up with the most straightforward methods discussed. The replace() and re.sub() function can remove a specific word from a string very easily. Other methods remove words from the start or end of the sentence. We also discussed how to remove duplicate words from a string in Python. The main approach to remove duplicate words was to split the string into an iterable, remove the duplicate items, and combine them into a string again.


import_contacts

You may also like:

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusion We use strings in python to handle text data. For processing the data, we sometimes need to […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusion While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways to remove HTML […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python. In this […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusion In this post, we will see how to convert UUID to String in […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusion In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of characters. In Python, […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.