AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Cases and lists

Week 8 · 1.5 Programming with data · 3 lessons
1

One case for all

Computing 9: Topic 1.5, pp. 89–118
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.

TextAfter .lower()After .upper()
"Rain""rain""RAIN"
"SUN""sun""SUN"
"Room 4B""room 4b""ROOM 4B"
"ok!""ok!""OK!"
Worked examples
Python
1answer = input("Capital of Egypt? ")
2answer = answer.lower()
3if answer == "cairo":
4    print("Correct")
5else:
6    print("Not quite")
Output
Correct
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.
Python
1word = "Hello"
2word.lower()
3print(word)
4word = word.lower()
5print(word)
Output
Hello
hello
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.
CallValue
"RAIN".lower()?
"Rain".upper()?
"a1b2".upper()?
"Sun".lower()?
"ok!".upper()?
2
What does this program print when the user types START, Start, s and stop?
Python
1reply = input("Ready? ")
2if reply.lower() == "start":
3    print("Go")
4else:
5    print("Wait")
3
This line stops with an error. Give the message, and correct the line.
Python
1print("tea".uppercase())
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?