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.
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 functionstring1 = 'Hello'string2 = 'World'difference(string1, string2)
difference()
that takes in two strings to find the uncommon words between them as parameters.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.
split(separator, maxsplit)
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.maxsplit+1
items.def difference(string1, string2):string1 = string1.split()string2 = string2.split()print('The strings are split successfully.')# Driver code to call a functionstring1 = 'Hello'string2 = 'World'difference(string1, string2)
string1
and string2
into a list of words each. We consider each whitespace a point of the split.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 itemsstring1 = string1.split()string2 = string2.split()A = set(string1) # Store all string1 list items in set AB = set(string2) # Store all string2 list items in set Bprint('The lists are converted into set objects successfully.')# Driver code to call a functionstring1 = 'Hello'string2 = 'World'difference(string1, string2)
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.
A.symmetric_difference(B)
Here, A and B are two sets.
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.
def difference(string1, string2):# Split both strings into list itemsstring1 = string1.split()string2 = string2.split()A = set(string1) # Store all string1 list items in set AB = set(string2) # Store all string2 list items in set Bstr_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 functionstring1 = 'Hello World'string2 = 'Hi World'difference(string1, string2)
symmetric_difference()
function with sets A and B as parameters, and store the output in a variable.isEmpty
variable and use the len()
function to return the number of objects uncommon in both sets. Then, we check if it equals zero.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.Let’s input the sample strings and test the program.
def difference(string1, string2):# Split both strings into list itemsstring1 = string1.split()string2 = string2.split()A = set(string1) # Store all string1 list items in set AB = set(string2) # Store all string2 list items in set Bstr_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 functionusr_str1 = 'Educative is good'usr_str2 = 'Educative is bad'output = difference(usr_str1, usr_str2)