If string contains function

Hi there. Im wondering how exactly the solution to my current exercise works.

def contains(big_string, little_string):
  return little_string in big_string

The function works perfectly, and correctly tells me True of False depending on if little_string is within big_string, but I have no idea how. There are no if or for commands. Im used to using the ‘in’ command when im putting it in a ‘for’ loop. but I have never been shown how this works.

Cheers for any help :slight_smile:

This suggests to us that you used a solution from somewhere else that you do not fully understand. That’s a no-no, even if we think we can learn from it. No matter; it’s good that you ask.

a in b

is a boolean expression that uses the in keyword as a membership test. No if required since it is a valid expression and yields a valid response… True or False.

It is what is known as a, predicate, and when it is the return of a function, that function is known as a, predicate function.

return a in b
1 Like

Thank you. Yeah I pressed on the “View Solution” option on the class because I stuck.

If im honest I don’t think that I have been taught how to do a “return a in b” yet, otherwise I most likely would of remembered.

So just to be clear, I can use that line of code, the “return a in b” to find out if a smaller string is hidden within a larger string?

Cheers for the help.

Yes, though we should be using the variables prescribed in the instructions in case the lesson checker is expecting them.