Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison in python words ending with

Tags:

python

I have a set of words as follows:

['Hey, how are you?\n','My name is Mathews.\n','I hate vegetables\n','French fries came out soggy\n']

In the above sentences i need to identify all sentences ending with ? or . or 'gy'. and print the final word.

My approach is as follows:

# words will contain the string i have pasted above.
word = [w for w in words if re.search('(?|.|gy)$', w)]
for i in word:
    print i

The result i get is:

Hey, how are you?

My name is Mathews.

I hate vegetables

French fries came out soggy

The expected result is:

you?

Mathews.

soggy

like image 848
Sharon Watinsan Avatar asked Aug 01 '13 04:08

Sharon Watinsan


1 Answers

Use endswith() method.

>>> for line in testList:
        for word in line.split():
            if word.endswith(('?', '.', 'gy')) :
                print word

Output:

you?
Mathews.
soggy
like image 111
Sukrit Kalra Avatar answered Sep 18 '22 21:09

Sukrit Kalra