How to Make a String Uppercase in Python with upper()
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
<string>.upper()
| Part | What 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
code = "spring25"
print(code.upper())
print(code)
entered = " Spring25 "
print(entered.strip().upper() == "SPRING25")

code.upper()makes the capitals version, and the next line printscodeitself, to show it hasn't changed.enteredis 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
uv run python upper.py
SPRING25
spring25
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
| Mistake | What happens | Do this instead |
|---|---|---|
Calling code.upper() and expecting code to change | code stays spring25 | Keep the result: code = code.upper() |
Calling upper() on None | AttributeError: '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 length | Use casefold() on both sides for case-insensitive comparisons |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A typed code doesn't match although it looks right | Spaces around it, or different capitals | Compare 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
- str.upper — the method and its Unicode rules.
- str.casefold — comparing text without case.
