• We just launched and are currently in beta. Join us as we build and grow the community.

How to Remove Duplicate Word from a String in Python

figue40

Voice Search Optimizer
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
108
Likes
128
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, we will learn how to program "How to Remove Duplicate Words from a String in Python." We will focus on identifying and removing any duplicate words from a string. The objective is to safely and accurately detect duplicate words within the string and remove them. A sample program will be provided to demonstrate the coding process.

This topic is very easy to understand. Just follow the instructions I provide, and you'll be able to do it yourself with ease. The program I’ll show you demonstrates the proper way to remove duplicate words from a string. I’ll also provide a simple and efficient method to achieve this. So, let's start coding!

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. def

    removeDupe(

    input

    )

    :

  3. regex =

    r'\b

    (\w

    +)(?:\W

    +\1

    \b

    )+'

  4. return

    re

    .sub

    (

    regex,

    r'\1

    '

    ,

    input

    ,

    flags=

    re

    .IGNORECASE

    )


  5. ret =

    False

  6. while

    True

    :
  7. print

    (

    "\n

    ================= Remove Duplicate Word from a String =================\n

    \n

    "

    )

  8. my_str =

    "Lets enjoy the code code"

  9. print

    (

    "Original String: "

    ,

    my_str)

  10. print

    (

    "\n

    Removing Duplicate word: "

    ,

    removeDupe(

    my_str)

    )


  11. opt =

    input

    (

    "\n

    Do you want to try again?(yes/no): "

    )

  12. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  13. ret=

    False
  14. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  15. ret=

    True
  16. print

    (

    "Exiting program...."

    )

  17. else

    :
  18. print

    (

    "Please enter yes/no:"

    )
  19. break

  20. if

    ret ==

    False

    :
  21. continue

This Python script removes duplicate words from a given string using regular expressions. The removeDupe() function defines a regex pattern that matches consecutive duplicate words, ignoring case, and replaces them with just one instance of the word. The program demonstrates this with a predefined string "Lets enjoy the code code", prints both the original and the modified string, and then prompts the user to try again or exit. It continues in a loop until the user decides to stop.

Output:

how-to-remove-duplicate-word-from-a-string-in-python-1.jpg

There you have it we successfully created How to Remove Duplicate Word from a String 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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

329,258

329,266

Top