Lessons · Python · one case to compare in
One case to compare in
s.lower() returns a new copy of the text in lowercase; the original string is untouched.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
People type Alex, alex and ALEX and mean the same person. Lowercasing both sides before comparing is how logins, searches and deduplication stop caring about capitals.
How to think about it
Normalise, then compare. Lowercase (or uppercase) both values at the moment you compare or store them, and remember the method gives you a new string to keep.
Worked example
name = "Hello"Mixed case.
print(name.lower())hello.
print(name)Hello: unchanged, because strings never change in place.
print("ALEX".lower() == "alex")True: the comparison the login screen needs.key = name.lower()Keep the result under a name, or it is gone.
Your turn
Match emails regardless of capitals.
same = a.() == b.lower()
Solve one with the tests running
The trap
Calling name.lower() and then using name expects a change that never happened. Assign the result: name = name.lower().
Practise one case to compare in on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.