How to return the difference of two strings in Python

Strings in Python

Strings are sequences of characters or collections of bytes that represent Unicode characters.

We can put the characters inside single or double quotation marks to make a string. Even triple quotation marks are possible in Python, but we only often use them to denote multiline texts and docstrings.

Creating a function

Let’s define the difference() function that takes two strings as parameters.

def difference(string1, string2):
print(' A function is define successfully.')
# Driver code to call a function
string1 = 'Hello'
string2 = 'World'
difference(string1, string2)

Explanation

  • Line 1: We define a function named difference() that takes in two strings to find the uncommon words between them as parameters.

Splitting the strings

Let’s use the split() function, a built-in string function in Python, to break up a string into a list where each word is a list item. It breaks the string at the specified separator.

Syntax

split(separator, maxsplit)

Parameters

There are two parameters of the split() function:

  • separator (optional): This is the delimiter where the split occurs. If we don’t provide this, the function uses whitespace by default.
  • maxsplit (optional): This specifies the number of splits we perform.
    • If we specify this, the list has the maximum number of maxsplit+1 items.
    • If we don’t specify this, there is no limit on the number of splits.

Code

def difference(string1, string2):
string1 = string1.split()
string2 = string2.split()
print('The strings are split successfully.')
# Driver code to call a function
string1 = 'Hello'
string2 = 'World'
difference(string1, string2)

Explanation

  • Lines 2–3: We split string1 and string2 into a list of words each. We consider each whitespace a point of the split.

Comparing the two sets

We use the set() built-in function in Python to store multiple items in a single variable. It is an unordered collection of items with no duplicates.

def difference(string1, string2):
# Split both strings into list items
string1 = string1.split()
string2 = string2.split()
A = set(string1) # Store all string1 list items in set A
B = set(string2) # Store all string2 list items in set B
print('The lists are converted into set objects successfully.')
# Driver code to call a function
string1 = 'Hello'
string2 = 'World'
difference(string1, string2)

Explanation

  • Lines 6–7: We convert both lists into set objects with distinct iterable elements.

Example

Let’s use symmetric_difference(), a built-in function in Python, to compare the two sets. This function returns all the items that are not identical in both sets.

Syntax

A.symmetric_difference(B)

Here, A and B are two sets.

A Venn diagram representation of the symmetric_difference() function
A Venn diagram representation of the symmetric_difference() function

The image above is a pictorial representation of the symmetric_difference function. The yellow-shaded area shows the items that are uncommon in Set B, and the blue-shaded area shows the items which are uncommon in Set A.

Code

def difference(string1, string2):
# Split both strings into list items
string1 = string1.split()
string2 = string2.split()
A = set(string1) # Store all string1 list items in set A
B = set(string2) # Store all string2 list items in set B
str_diff = A.symmetric_difference(B)
isEmpty = (len(str_diff) == 0)
if isEmpty:
print("No Difference. Both Strings Are Same")
else:
print("The Difference Between Two Strings: ")
print(str_diff)
print('The difference is shown successfully.')
# Driver code to call a function
string1 = 'Hello World'
string2 = 'Hi World'
difference(string1, string2)

Explanation

  • Line 9: We call the symmetric_difference() function with sets A and B as parameters, and store the output in a variable.
  • Line 10: We create an isEmpty variable and use the len() function to return the number of objects uncommon in both sets. Then, we check if it equals zero.
  • Lines 12–16: We use an if-else statement to check if the isEmpty variable is True, which means both the strings are equal. Otherwise, we call the function to print the uncommon words as the output.

Program testing

Let’s input the sample strings and test the program.

def difference(string1, string2):
# Split both strings into list items
string1 = string1.split()
string2 = string2.split()
A = set(string1) # Store all string1 list items in set A
B = set(string2) # Store all string2 list items in set B
str_diff = A.symmetric_difference(B)
isEmpty = (len(str_diff) == 0)
if isEmpty:
print("No Difference. Both Strings Are Same")
else:
print("The Difference Between Two Strings: ")
print(str_diff)
print('The programs runs successfully.')
# Driver code to call a function
usr_str1 = 'Educative is good'
usr_str2 = 'Educative is bad'
output = difference(usr_str1, usr_str2)

Explanation

  • Lines 19–20: We input two strings with a one-word difference.
  • Line 21: We call the function, and store the output in a variable.

Free Resources