Python Reference · String Methods
Python str.upper() method
Reference for Python str.upper(): convert a string to upper case, why it returns a new string, Unicode quirks such as the German sharp s, and common uses.
The upper() method returns a copy of the string with every cased character converted to upper case. Digits, punctuation and other characters without an upper-case form are left alone. The original string is never modified, because strings are immutable.
Typical uses are shouting a warning, building constants or codes such as "ab-12".upper(), and normalizing input before comparing it.
print("Hello, world!".upper()) # HELLO, WORLD!
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
str.upper()
This method takes no parameters.
Return value: a new string in upper case. If the text has no lower-case letters, CPython may return the same object, so do not rely on identity.
Raises: nothing.
Usage notes
- Returns a copy. Writing
s.upper()on its own line has no effect. Uses = s.upper(). - Unicode aware. Accented and non-Latin letters are converted using the Unicode case mappings.
- Length can change. Some characters expand:
"ß".upper()is"SS"and the ligature"fi"becomes"FI". Do not assume the result has the same length or index positions as the input. - Not a round trip.
s.upper().lower()does not always give backs(for instance afterßbecomesSS). - Locale independent. The result does not depend on the machine's locale. Languages with special rules, such as Turkish dotted and dotless i, need custom handling.
- Use
casefold()for caseless comparison. Comparinga.upper() == b.upper()works for many cases butcasefold()is the tool designed for it. - Bytes.
bytes.upper()changes only ASCII letters. - Check without converting.
s.isupper()tells you whether the cased characters are all upper case.