Skip to main content

How to Make a String Uppercase in Python with upper()

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Codes, IDs and abbreviations are often stored in capitals, while people type them any way they like. upper() turns a string into capitals so the two can be compared. This guide makes a discount code uppercase and checks a typed one, with real output from Python 3.15.2.

Syntax​

Syntax
<string>.upper()
PartWhat it means
<string>Any string
.upper()Returns a new string with every letter in capitals. The original stays as it was

Python's documentation: "Return a copy of the string with all the cased characters converted to uppercase."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

upper.py
code = "spring25"
print(code.upper())
print(code)

entered = " Spring25 "
print(entered.strip().upper() == "SPRING25")

The upper.py script in a macOS-style code window: it prints spring25 in uppercase, prints the original code again, then strips and uppercases a typed code with spaces around it and compares it with SPRING25.

  • code.upper() makes the capitals version, and the next line prints code itself, to show it hasn't changed.
  • entered is what someone might type: mixed case, with spaces around it.
  • entered.strip().upper() removes the spaces, then makes it uppercase, before comparing.

Step 2: Run It​

Terminal
uv run python upper.py
Output
SPRING25
spring25
True

The output in a terminal: SPRING25, then spring25, then True.

upper() gave SPRING25, and the digits stayed as they were. The second line shows code is still spring25: upper() made a new string. The typed " Spring25 " matched once it was stripped and made uppercase.

Common Mistakes​

MistakeWhat happensDo this instead
Calling code.upper() and expecting code to changecode stays spring25Keep the result: code = code.upper()
Calling upper() on NoneAttributeError: 'NoneType' object has no attribute 'upper'Check for None before you call it
Using upper() to compare any text, whatever the language"Straße".upper() is "STRASSE", so some words change lengthUse casefold() on both sides for case-insensitive comparisons

Troubleshooting​

SymptomLikely causeFix
A typed code doesn't match although it looks rightSpaces around it, or different capitalsCompare entered.strip().upper()

FAQ​

What's the opposite of upper()?​

lower(): "Spring25".lower() gives "spring25".

What does casefold() do differently?​

It's made for comparing text without caring about case, across languages: "Straße".casefold() == "STRASSE".casefold() is True.

Conclusion​

"spring25".upper() returned SPRING25 and left the original alone, and strip().upper() let a typed " Spring25 " match the stored code.

Additional Resources​