Reputation: 9643
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
Reputation: 26150
You got the order backwards.
if word[-1] in ".?":
Refer to the in
operator in the documentation
Upvotes: 3
Reputation: 353009
For this particular case, where you want to check the end:
if word.endswith(('.','?')):
Upvotes: 3