I've come across a situation where two solutions are proposed.
Let's say I have a string, "foo bar" as an input and I need to check that it ends in "bar".
One person's solution is this:
is_bar = lambda x: x[-3:] == "bar"
The other person's solution is this:
is_bar = lambda x: x.endswith("bar")
Can anyone provide insight into the differences between the two and which is preferred? Assume only the one suffix "bar" is ever preferred, as endswith
can have tuples of suffixes and would be preferred if there were more.