Why does this work?

def check_for_name(sentence, name):
return sentence.lower().find(name.lower()) != -1

what does the != -1 do specifically?

this from the CODE CHALLENGE: STRING METHODS

Hi there, welcome to the forums!

Here is the Python documentation on the string find() method. You ought to be able to work out, from that, what purpose the != -1 code serves.

Spoiler Alert!

str.find() returns the index of the first occurrence of the target substring. So, "python".find("th") returns 2.

If the substring doesn’t exist inside the string, .find() returns -1.

1 Like

Thank you very much, I was able to work it out. I appreciate the clarification, and also teaching me where to find documentation for python.

1 Like