Tom
Tom

Reputation: 9643

python - "if in" statement for multiple possibilities

I want to check if a question mark or a dot are in the last position of a word. So doing something like:

if ".","?" in word[-1]:

Or:

if [".","?"] in word[-1]:

Result in an invalid syntax. What is the right way to do this? (preferably without regex)

Upvotes: 0

Views: 124

Answers (2)

Silas Ray
Silas Ray

Reputation: 26150

You got the order backwards.

if word[-1] in ".?":

Refer to the in operator in the documentation

Upvotes: 3

DSM
DSM

Reputation: 353009

For this particular case, where you want to check the end:

if word.endswith(('.','?')):

Upvotes: 3

Related Questions