str.upper() / .lower()
Python · Strings
Theory
.upper() returns a copy of a string with every letter converted to uppercase, and .lower() returns a copy in lowercase; digits, spaces and symbols stay as they are. With name = 'Asha Rao', name.upper() gives 'ASHA RAO'. Strings in Python are immutable, so these methods never change name itself: they build and return a new string, and if you do not store the result (name = name.upper()) it is simply lost.
Their main job in data work is normalising text so comparisons ignore case. 'Pune' == 'PUNE' is False, but 'Pune'.lower() == 'PUNE'.lower() is True. Related methods: .title() capitalises each word, .capitalize() only the first letter, .swapcase() flips each letter. For caseless matching of international text, .casefold() is stronger than .lower(): 'Straße'.casefold() gives 'strasse'.
Why we use it
Real data is typed by humans: 'pune', 'Pune' and 'PUNE' all mean the same city. Converting to one case before comparing, grouping or deduplicating prevents the same value from being counted as three.
When to use it
Use .lower() (or .casefold()) before comparing or grouping user-entered text, and .upper() for codes like state or country codes. Do not use them on text where case carries meaning, such as passwords or case-sensitive IDs.
How it works
- Call name.upper()
- Read the original string 'Asha Rao' (it is never modified)
- Walk through each character
- Is the character a lowercase letter? Yes: Use its uppercase form. No: Copy it unchanged.
- Build a new string from the results
- Return 'ASHA RAO'; name is still 'Asha Rao'
Syntax
string.upper()
string.lower()Example
name = 'Asha Rao'
print(name.upper())
print(name.lower())Key points
- Neither modifies the original string; strings are immutable
- Store the result: name = name.upper()
- Useful for case-insensitive comparisons: a.lower() == b.lower()
- .casefold() is the stronger choice for international text
- Non-letters such as digits and spaces are left unchanged