USING THE SPLIT() FUNCTION TO COMPARE WORDS
Listing 2.4 shows the content of Compare2.py, which illustrates how to use the split() function to compare each word in a text string with another word.
Listing 2.4: Compare2.py
x = 'This is a string that contains abc and Abc'
y = 'abc'
identical = 0
casematch = 0
for w in x.split():
if(w == y):
identical = identical + 1
elif (w.lower() == y.lower()):
casematch = casematch + 1
if(identical > 0):
print('found identical matches:', identical)
if(casematch > 0):
print('found case matches:', casematch)
if(casematch == 0 and identical == 0):
print('no matches found')
Listing 2.4 uses the split() function to compare each word in the string x with the word abc. If there is an exact match, the variable identical is incremented. If a match does not occur, a case-insensitive match of the current word is performed with the...