How to Remove Duplicate Words from a Sentence Using Regular Expressions in Python

In this tutorial, we’ll learn how to program “How to Remove Duplicate Words from a Sentence Using Regular Expressions in Python.” The objective is to safely remove all duplicate words from a sentence. This tutorial will guide you through the process step by step. So, let’s get started!

This topic is straightforward to understand. Just follow the instructions I provide, and you’ll complete it with ease. The program I’ll demonstrate will illustrate the proper way to find and remove all duplicate words from a sentence. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import re
  2.  
  3.  
  4. def removeDuplicateWords(input):
  5.  
  6.     regex = r'\b(\w+)(?:\W+\1\b)+'
  7.  
  8.     return re.sub(regex, r'\1', input, flags=re.IGNORECASE)
  9.  
  10. while True:
  11.     print("\n================= Remove Duplicate Words from a Sentence =================\n\n")
  12.  
  13.  
  14.     my_string = input("Enter your sentence: ")
  15.     print(removeDuplicateWords(my_string))
  16.  
  17.     opt = input("\nDo you want to try again?(yes/no): ")
  18.  
  19.     if opt.lower() == 'yes':
  20.             ret=False
  21.     elif opt.lower() == 'no':
  22.             ret=True
  23.             print("Exiting program....")
  24.     else:
  25.             print("Please enter yes/no:")
  26.             break
  27.  
  28.     if ret == False:
  29.             continue

This program removes duplicate words from a sentence using regular expressions. It detects repeated words regardless of case and replaces them with a single occurrence. The user can input a sentence, see the cleaned-up result, and choose to try again or exit the program.

Output:

There you have it we successfully created How to Remove Duplicate Words from a Sentence Using Regular Expressions in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment