Check_name - why does the code work?


I am on the check_names exercise and I had to look at the code solution. I don’t understand how or why the code works. Can someone please explain it me? Specifically why .lower() was used?

Here’s the brief:

Check Name




Write a function called check_for_name that takes two strings as parameters named sentence and name . The function should return True if name appears in sentence in all lowercase letters, all uppercase letters, or with any mix of uppercase and lowercase letters. The function should return False otherwise.

For example, the following three calls should all return True :

check_for_name("My name is Jamie", "Jamie")check_for_name("My name is jamie", "Jamie")check_for_name("My name is JAMIE", "Jamie")

And this is the solution code:

# Write your check_for_name function here:

def check_for_name(sentence, name):

  return name.lower() in sentence.lower()

# Uncomment these function calls to test your function:

print(check_for_name("My name is Jamie", "Jamie"))

# should print True

print(check_for_name("My name is jamie", "Jamie"))

# should print True

print(check_for_name("My name is Samantha", "Jamie"))

# should print False

When seeing if one string is in another, if the case of the name does not matter, the easiest thing to do is often convert both to lower case.
Using .lower() on both name and sentence would mean both are entirely lower case letters.

string = "TeRRiBle CaSEed senTENcE"
string = string.lower()
# string = "terrible cased sentence" 
#  Which is much easier to work with

Than the in operator return True or False depending on if the value on it left is in the value on its right.

>>> "i" in "high"

>>> 1 in [3, 5, 7, 1, 9]

>>> "H" in "high"

>>> "H".lower() in "high"