For example, we want to detect "does a string end with a specific string?", we usually use:
If Needle.endswith (' ly ') or Needle.endswith (' Ed ') or needle.endswith (' ing ') or needle.endswith (' ers '): print (' is valid ') else: print (' Invalid ')
Pretty ugly, huh? If we detect whether the variable needle is one of the following specific strings, this will be written:
If needle in (' ly ', ' ed ', ' ing ', ' ers '): print (' is valid ') else: print (' Invalid ')
However, we cannot use in in the EndsWith function, but let's change the idea that we need to check whether the end of a string is any of the following strings, and we'll find that Python has an intrinsic function any, so our code can be changed to:
If any ([Needle.endswith (e) to E in (' ly ', ' ed ', ' ing ', ' ers ')]): print (' is valid ') else: print (' Invalid ')
I believe many readers will disagree with me, or have a better way of writing. But that's no longer important. I understand that most of you will use similar wording to face this similar problem. My real purpose is to show the elegance of Python.