String comparison in python words ending with -
i have set of words follows:
['hey, how you?\n','my name mathews.\n','i hate vegetables\n','french fries came out soggy\n'] in above sentences need identify sentences ending ? or . or 'gy'. , print final word.
my approach follows:
# words contain string have pasted above. word = [w w in words if re.search('(?|.|gy)$', w)] in word: print the result is:
hey, how you?
my name mathews.
i hate vegetables
french fries came out soggy
the expected result is:
you?
mathews.
soggy
use endswith() method.
>>> line in testlist: word in line.split(): if word.endswith(('?', '.', 'gy')) : print word output:
you? mathews. soggy
Comments
Post a Comment