Program to find and print uncommon words from two strings using Python

Introduction

In this section of programming, we will find and print the uncommon words from two given strings.

Program to find and print uncommon words from two strings

Program

def uncomn_wrd(x,y):
    x = x.split()
    y = y.split()
# Using symmetric_difference()
    k = set(x).symmetric_difference(set(y))
    return k


str1 = input("Enter first string : ")
str2 = input("Enter second string : ")
print("Uncommon words are :", list(uncomn_wrd(str1, str2)))

Output

Program to find and print uncommon words from two strings Output

Explanation

In general, to find the uncommon words we can make a hash table of words as key and print out the words that occurs exactly once.

In our coding we have used a python in-built function symmetric_difference() which will list out the set of words from both the input strings except the common one’s.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.