Python program to find uncommon words from two string

Here, we will take two strings from the user and then using a Python program, we will print all the characters that are not common in both the strings.
Submitted by Shivang Yadav, on April 28, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

Find uncommon words from two string

We will take two strings as input from users consisting of words. And then we will print all words from the string that are not present in both the strings.

The string consists of sentences that are space-separated words.

Example:

Input:
str1 = "learn programming at includehelp"
str2 = "learn python programming language"

Output:
"python", "at", "includehelp"

A simple method to solve the problem is by finding words that occur only once in any of the strings.

For this, we will create a hashmap by which will store the words and their frequency of occurrence for both strings. And then print those words from the hashmap whose occurrence count is one.

Program to find uncommon words from two string

# Python program to find uncommon words from two string,

# Getting strings as input from the user 
str1 = input('Enter first string : ')
str2 = input('Enter second string : ')

# finding uncommon words
count = {}
for word in str1.split():
    count[word] = count.get(word, 0) + 1
for word in str2.split():
    count[word] = count.get(word, 0) + 1
  
uncommonWords =  [word for word in count if count[word] == 1]

# printing uncommon words 
print("All uncommon words from both the string are ", uncommonWords)

Output:

Enter first string : learn python programming 
Enter second string : learn programming at includehelp
All uncommon words from both the string are  ['python', 'at', 'includehelp']

Alternate method

Another way to solve the problem is by storing both the strings as lists and then store the characters from one list that are not present in another to a list. Then print all values that are uncommon for both the strings.

Program to find uncommon words from two string,

# Python program to find uncommon words from two string,

# Getting strings as input from the user 
str1 = input('Enter first string : ')
str2 = input('Enter second string : ')

# finding uncommon words
str1List = str1.split()
str2List = str2.split()
uncommonWords = ''
for words in str1List:
    if words not in str2List:
        uncommonWords = uncommonWords+" "+words
for words in str2List:
   if words not in str1List:
        uncommonWords = uncommonWords+" "+words
  
# printing uncommon words 
print("All uncommon words from both the string are ", uncommonWords)

Output:

Enter first string : learn python programming language
Enter second string : learn programming at includehelp
All uncommon words from both the string are   python language at includehelp

Method 2: Using built-in Python function

Python provides a built-in function to perform this task. It is symmetric_difference() on sets.

Program to find uncommon words from two strings

# Python program to find uncommon words from two string,

# Getting strings as input from the user 
str1 = input('Enter first string : ')
str2 = input('Enter second string : ')

# finding uncommon words
str1Coll = str1.split()
str2Coll = str2.split()
uncommonWords = set(str1Coll).symmetric_difference(set(str2Coll))
  
# printing uncommon words 
print("All uncommon words from both the string are ", uncommonWords)

Output:

Enter first string : python programming language
Enter second string : learn python programming at includehelp
All uncommon words from both the string are  {'language', 'at', 'learn', includehelp}

Python String Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.