Q:

Python program to find uncommon words from two string

belongs to collection: Python String Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to print URL from a string... >>
<< Python program to execute Python code from the str...