USING THE SPLIT() FUNCTION TO COMPARE TEXT STRINGS
Listing 2.7 shows the content of CompareStrings1.py, which illustrates how to determine whether the words in one text string are also words in a second text string.
Listing 2.7: CompareStrings1.py
text1 = 'a b c d'
text2 = 'a b c e d'
if(text2.find(text1) >= 0):
print('text1 is a substring of text2')
else:
print('text1 is not a substring of text2')
subStr = True
for w in text1.split():
if(text2.find(w) == -1):
subStr = False
break
if(subStr == True):
print('Every word in text1 is a word in text2')
else:
print('Not every word in text1 is a word in text2')
Listing 2.7 initializes the string variables text1 and text2, and uses conditional logic to determine whether text1 is a substring of text2 (and then prints a suitable message).
The next part of Listing 2.7 is a loop that iterates through the words...