Search code examples
pythonpython-3.xends-with

How can I do variable.endswith("a" or "b" or "c"....) : doAThing in Python?


I'm working on a small piece of translation software in its early stages. I'm trying to take the end character of a word and do something with it, but I need to differentiate between different cases.

How can I use if endswith("a" or "b" or "c") : doAThing?

I've tried ||, |=, and or.

if word.endswith("a" , "e" , "i" , "o" , "u") : print("JEFF")

Error:

 File "test.py", line 50, in <module>
    if word.endswith("a" , "e" , "i" , "o" , "u") and not ("a" , "e" , "i" , "o" , "u"): print("JEFF")
TypeError: endswith() takes at most 3 arguments (5 given)

Solution

  • If you check the docs for the 'endswith' function, you'll see the function signature. It can take either one string or a tuple of strings to match.

    The way you are passing in the arguments is as 5 different args. To make those one tuple (and one argument), surround the strings in parenthesis.

    if word.endswith(("a" , "e" , "i" , "o" , "u")):
        print("JEFF")