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

How Find Uncommon Words from Two Strings in Python

arnau2010

Online Campaign Manager
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
113
Likes
86
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, we’ll learn how to program "How to Find Uncommon Words from Two Strings in Python." The objective is to safely and efficiently identify words that appear in one string but not in the other. This tutorial will guide you through the process step by step, ensuring a clear understanding of all necessary elements for extracting uncommon words. 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 show you the proper way to find uncommon words from two strings. 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. def

    UncommonWords(

    A,

    B)

    :


  2. count =

    {

    }


  3. for

    word in

    A.split

    (

    )

    :
  4. count[

    word]

    =

    count.get

    (

    word,

    0

    )

    + 1


  5. for

    word in

    B.split

    (

    )

    :
  6. count[

    word]

    =

    count.get

    (

    word,

    0

    )

    + 1


  7. return

    [

    word for

    word in

    count if

    count[

    word]

    ==

    1

    ]


  8. while

    True

    :
  9. print

    (

    "\n

    ================= Find Uncommon Words from Two Strings =================\n

    \n

    "

    )


  10. stringA =

    input

    (

    "Enter a string A: "

    )
  11. stringB =

    input

    (

    "Enter a string B: "

    )


  12. print

    (

    UncommonWords(

    stringA,

    stringB)

    )

  13. opt =

    input

    (

    "\n

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

    )

  14. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  15. ret=

    False
  16. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  17. ret=

    True
  18. print

    (

    "Exiting program...."

    )
  19. else

    :
  20. print

    (

    "Please enter yes/no:"

    )
  21. break

  22. if

    ret ==

    False

    :
  23. continue

This program finds uncommon words from two input strings. An uncommon word is a word that appears in only one of the two strings but not in both. It works by counting the occurrences of each word in both strings and returning words that appear exactly once. The program runs in a loop, allowing users to input multiple sets of strings until they choose to exit.

Output:

how-find-uncommon-words-from-two-strings-in-python-1.jpg

There you have it we successfully created How Find Uncommon Words from Two Strings 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 hidden text.
 

442,401

317,942

317,951

Top