In this exercise, the same_name() function will compare the two names to see if they are equal. How can this be done so that it will ignore any differences in capitalization?
Answer
This can be done by converting the strings to all lower-case using lower() OR all upper-case using upper() before performing the equality test. The upper() and lower() methods are called on the string variables. For example, to compare the strings as lower-case, the check can be written as:
str.lower() is a function built into python. You can use the below code in place of the code provided by ajaxninja66418 and get the same results.
Now I’m not sure if there are any benefits to using one over the other.
You can do this with either the .upper() or .lower() method. Essentially you want to “standardize” the characters so that they are all either lowercase or uppercase. This ensures the function compares the character regardless of case. I used .upper for my code.
def same_name(your_name, my_name):
#standardize both strings to uppercase so that function compares strings without regard to case deviations.
if (your_name.upper() == my_name.upper()):
return True
else:
return False
Is used to convert strings to lower (or upper) case letters.
Are there other ways of converting strings using the “string.xxxx()” method?
Also what are theses called when you use the " . " to change a string?