GoalUse .upper() and .lower() so that a program accepts an answer however the user types the capital letters.
New words
case · whether a letter is a capital (uppercase) or a small letter (lowercase).lower() · a string method that gives a copy of the text with all letters in lowercase.upper() · a string method that gives a copy of the text with all letters in uppercase
Explanation
In Python "Rain", "RAIN" and "rain" are three different strings, so a comparison with == treats them as different answers. The user might type any of them. The easy fix is to change the case before comparing. The method .lower() gives the text in small letters, and .upper() gives it in capitals. You write the string, a full stop and the method with brackets, such as answer.lower(). Both only change letters; digits and symbols stay the same. A method does not change the original variable. It gives back a new string, so you must store it, for example answer = answer.lower(), or use it straight away in the condition.
A quiz answer that ignores capitals. The user types CAIRO. The program changes the answer to lowercase and then compares it with "cairo". Cairo, cairo and CAIRO all print Correct. Giza prints Not quite.
Python
1code = input("Code? ").upper()
2print(code)
Output
AB12X
A code in capitals. The user types ab12x. The digits 1 and 2 stay as they are, and only the letters change. The program was run with this input.
A common mistake. The line word.lower() creates a lowercase copy but does not store it, so word is still Hello on the first print. Only after word = word.lower() does the variable hold the new text.
Class activity
Case detectives (no computer). Write ten answers on cards in different styles, such as TEA, tea, Tea, tEa. Write the comparison on a big sheet: if drink == "tea":. First test each card as it is: which cards pass? Then pass every card through a ‘.lower()’ box, where you rewrite it in small letters, and test again. Do the same with ‘.upper()’ and the comparison "TEA". Add cards that include digits, such as Room 4b, and check that the digits do not change.
Practice
1
Give the value of each call.
Call
Value
"RAIN".lower()
?
"Rain".upper()
?
"a1b2".upper()
?
"Sun".lower()
?
"ok!".upper()
?
"RAIN".lower() is "rain". "Rain".upper() is "RAIN". "a1b2".upper() is "A1B2". "Sun".lower() is "sun". "ok!".upper() is "OK!".
2
What does this program print when the user types START, Start, s and stop?
START: Go. Start: Go. s: Wait, because "s" is not "start". stop: Wait.
3
This line stops with an error. Give the message, and correct the line.
Python
1print("tea".uppercase())
AttributeError: 'str' object has no attribute 'uppercase'. The method is called upper, so write print("tea".upper()).
4
Write a program that asks for a name and prints only its first letter as a capital letter. What does it print for amir?
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): name = input("Name? ") / print(name[0].upper()). Check: For amir it prints A.