Module 7 · Strings — topic 9 of 22
upper()
upper() returns a copy of a string with every letter converted to upper case. Learn its behaviour with digits, symbols and Unicode, isupper(), and when upper-case normalisation is the right choice.
Learning objectives
- Convert text to upper case with upper()
- Test for upper case with isupper()
- Use upper() for codes and case-insensitive comparisons
Definition
upper() returns a copy of a string with every letter converted to upper case. Learn its behaviour with digits, symbols and Unicode, isupper(), and when upper-case normalisation is the right choice.
Introduction
upper() returns a new string in which all cased letters are capital letters. Digits, spaces and punctuation stay the same. "pnr 42a".upper() gives "PNR 42A".
Why it matters
Many codes are conventionally upper-case: PNRs, IFSC codes, vehicle numbers, country codes and SKU numbers. Converting user input with upper() lets people type in any case while your program stores one consistent form.
Detailed explanation
upper() follows the Unicode standard, so it also handles accented letters such as é → É and letters from other scripts that have case. Scripts without upper and lower case, such as Devanagari or Tamil, are simply left unchanged.
Related methods are useful to know: capitalize() makes only the first character upper case and title() capitalises the first letter of every word. They are better than upper() for names and headings.
upper() takes no arguments and returns a new string; the original is unchanged.
Only characters that have upper-case forms change. "123-abc!".upper() is "123-ABC!".
It works for Unicode letters too: "straße".upper() becomes "STRASSE", because the German ß has a two-letter upper-case form. The result can therefore be longer than the input.
isupper() returns True if the string has at least one cased letter and all cased letters are upper case. "ABC123".isupper() is True; "123".isupper() is False because there are no letters.
For comparing text without regard to case, lower() and casefold() are more common, but comparing two upper()-ed strings works the same way.
Upper-case text is harder to read in long passages, so use it for short codes and labels rather than sentences.
Syntax
- upper() returns a new string.
- Combine with strip() when cleaning input.
- isupper() tests the current case.
Code examples
Converting to upper case
PNR 42A 123-ABC! STRASSE
Testing with isupper()
'ABC123' True 'AbC' False '123' False 'HELLO WORLD' True
More examples
Normalising vehicle registration numbers
'ka 05 mn 1234' -> KA05MN1234 ' KA05MN1234 ' -> KA05MN1234 'Ka-05-Mn-1234' -> KA05MN1234 Unique vehicles: 1
Step-by-step explanation
- Each entry is converted to upper case, then spaces and hyphens are removed.
- Adding the cleaned values to a set keeps only unique registrations.
- {raw!r:>17} prints the original with quotes, right-aligned in 17 characters, so the arrows line up.
- All three differently typed entries turn out to be the same vehicle.
Expected output
Running “Normalising vehicle registration numbers” prints exactly:
Practical applications
- Railway and airline systems store PNRs in upper case.
- Banking forms upper-case IFSC codes before validating them.
- Inventory systems upper-case SKUs so searches match regardless of typing.
Best practices
- Upper-case short codes and identifiers on input.
- Combine upper() with strip() and replace() to normalise formats.
- Don’t upper-case long text meant for reading.
Common mistakes
Not saving the result
Avoid
Prefer
upper() doesn’t change code in place.
Assuming isupper() is True for digits
Avoid
Prefer
isupper() needs at least one cased letter to return True.
Key takeaways
- upper() returns an upper-case copy; non-letters are unchanged.
- isupper() is True only if there are letters and all are upper case.
- Use upper() to normalise codes typed in any case.
Try it yourself
- 1Ask for a 3-letter airport code and print it in upper case.
- 2Count how many words in a sentence are entirely upper case.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?