What upper() and lower() do
Python strings have two methods for changing letter case. upper() returns the string with every letter in uppercase, and lower() returns it with every letter in lowercase. Characters that are not letters, such as digits, spaces and punctuation, pass through unchanged.
word = "Hello World"
print(word.upper()) # HELLO WORLD
print(word.lower()) # hello world
print(word) # Hello World
name = "ALICE"
print(name.lower()) # alice
print("abc123".upper()) # ABC123
The third print shows that word itself is still Hello World. Strings are immutable in Python, which means a string cannot be modified after it is created. A method like upper() builds a new string and returns it, and the original is left alone. If you want to keep the result, you have to store it in a variable.
Why case conversion is useful
Python compares strings character by character, and an uppercase letter is a different character from its lowercase partner. So these two strings are not equal:
print("Yes" == "yes") # False
print("Yes".lower() == "yes") # True
print("Ab".lower() == "aB".lower()) # True
Converting both sides to the same case is the standard way to compare text while ignoring case, for instance when checking an answer typed by a user. Converting for display is the other common use: upper() for a heading or a code, lower() for normalising input before storing it.
Case methods combine well with other string methods, because each one returns a string. A typed answer with extra spaces and a different case can be cleaned and checked in one expression:
answer = " YES \n"
if answer.strip().lower() == "yes":
print("confirmed")
This prints confirmed. The call strip() removes the surrounding whitespace, lower() lowers the letters, and only then does the comparison happen.
Related methods
A few more case methods are worth knowing:
capitalize()makes the first character uppercase and the rest lowercase:"Hello World".capitalize()givesHello world.title()capitalises each word:"hello world".title()givesHello World. It treats an apostrophe as a word boundary, so"they're".title()givesThey'Re.swapcase()flips the case of every letter.isupper()andislower()do not convert anything. They answer whether the letters in a string are all uppercase or all lowercase, returningTrueorFalse.
For comparing text that may contain letters from many languages, casefold() is a stronger version of lower(). For example, "straße".casefold() gives strasse. For ordinary English text, lower() is enough.
Common mistakes
- Ignoring the result. A line like
word.upper()on its own does nothing visible. The uppercase string is created and discarded, andwordkeeps its old value. Writeword = word.upper()or assign to a new name. - Forgetting the parentheses.
word.upperis the method itself, not a converted string. Printing it shows something like a bound method, notHELLO WORLD. - Expecting digits to change. Numbers and symbols have no case.
"123".isupper()isFalse, because the string has no uppercase letters at all. - Comparing without converting.
"Yes" == "yes"isFalse. Convert both sides first.
The exercise, step by step
The lesson gives word set to "Hello World" and asks for two new variables, one in each case.
- Create the uppercase version. Call
word.upper()and assign the result to a variable namedupper. It holds"HELLO WORLD". - Create the lowercase version. Call
word.lower()and assign the result to a variable namedlower. It holds"hello world".
word = "Hello World"
upper = word.upper()
lower = word.lower()
Both calls start from word, and neither changes it. That is why the second line can still use word and get the original mixed-case text: the first line did not modify it.
The names upper and lower are the same as the method names, which is fine. A variable name is just a label, and word.upper() is looked up on the string, not among your variables. The two do not interfere.
Key takeaways
s.upper()ands.lower()return a new string in uppercase or lowercase.- Only letters change. Digits, spaces and punctuation stay as they are.
- The original string is never modified, so store the result.
- Convert both strings to the same case before comparing text without regard to case.