Lesson goal: Comparing strings considering case

Previous: Comparing strings | Home | Next: Finding Palindromes

In the last lesson you learned how to compare a string to the word "computer." What would happen if the person typed the word "Computer" or "COMPUTER" or "CoMpUtEr"? Should these compare favorably to "computer" as well? It depends if you want it to, but indeed "computer" is not equal to "Computer" or "COMPUTER." You are probably familiar with "case sensitive" passwords online, meaning upper and lower case letters are treated differently; this is the heart of the issue.

If you want to ignore the case of letters in a comparison, you first have to covert your incoming string to all upper or all lower case first. You can do this with the string.lower() or string.upper() functions.

Now you try. Run this code and type in the word computer with different cases, like Computer or COMPUTER.

Type your code here:

See your results here:

This code will work. Try typing the word "computer" at the prompt, with different variations of the case of the letters. It'll still work, since the entire string is converted to lowercase before doing the comparison. CHALLENGE: How would you change the code that would first convert the string in variable a to upper case? Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.