str.upper() / str.lower() / str.title()
| Since: | Python 2(2000) |
|---|
Methods for converting the case of a string. Commonly used to normalize user input or preprocess strings before comparison.
Syntax
# Converts all characters in the string to uppercase. str.upper() # Converts all characters in the string to lowercase. str.lower() # Converts the first character to uppercase and the rest to lowercase. str.capitalize() # Converts the first character of each word to uppercase and the rest to lowercase (title case). str.title() # Swaps uppercase characters to lowercase and lowercase characters to uppercase. str.swapcase()
Method List
| Method | Description |
|---|---|
| str.upper() | Returns a new string with all lowercase letters converted to uppercase. |
| str.lower() | Returns a new string with all uppercase letters converted to lowercase. |
| str.capitalize() | Returns a new string with the first character uppercased and the rest lowercased. |
| str.title() | Returns a new string with the first character of each word uppercased and the rest lowercased. |
| str.swapcase() | Returns a new string with uppercase characters converted to lowercase and vice versa. |
Sample Code
upper_lower_basic.py
text = "Hello, Python World!"
print(text.upper())
print(text.lower())
user_input = "Yes"
if user_input.lower() == "yes":
print("Approved.")
command = "QUIT"
if command.upper() in ["QUIT", "EXIT", "Q"]:
print("Exiting.")
Running the code produces the following output:
python3 upper_lower_basic.py HELLO, PYTHON WORLD! hello, python world! Approved. Exiting.
upper_lower_variants.py
s1 = "hello world"
print(s1.capitalize())
s2 = "HELLO WORLD"
print(s2.capitalize())
book_title = "the quick brown fox"
print(book_title.title())
name = "it's a wonderful life"
print(name.title())
text2 = "Hello World"
print(text2.swapcase())
print("HELLO".isupper())
print("hello".islower())
print("Hello World".istitle())
Running the code produces the following output:
python3 upper_lower_variants.py Hello world Hello world The Quick Brown Fox It'S A Wonderful Life hELLO wORLD True True True
upper_lower_normalize.py
headers = [" NAME ", "AGE", "city", "EMAIL ADDRESS"]
normalized = [h.strip().lower().replace(" ", "_") for h in headers]
print(normalized)
username = " Iori_YAGAMI "
normalized_name = username.strip().lower()
print(normalized_name)
fighters = ["YAGAMI_IORI", "KUSANAGI_KYO", "TERRY_BOGARD", "MAI_SHIRANUI", "CHRIS"]
lower_names = [f.lower() for f in fighters]
print(lower_names)
Running the code produces the following output:
python3 upper_lower_normalize.py ['name', 'age', 'city', 'email_address'] iori_yagami ['yagami_iori', 'kusanagi_kyo', 'terry_bogard', 'mai_shiranui', 'chris']
Common Mistakes
Common Mistake 1: capitalize() lowercases everything except the first character
capitalize() not only uppercases the first character — it also lowercases all remaining characters. Be careful when using it on strings that already contain uppercase letters.
mistake1_ng.py
name = "yagami IORI" print(name.capitalize())
Running the code produces the following output:
python3 mistake1_ng.py Yagami iori
mistake1_ok.py
name = "yagami iori" words = name.split() result = " ".join(w.capitalize() for w in words) print(result)
Running the code produces the following output:
python3 mistake1_ok.py Yagami Iori
Common Mistake 2: Not knowing the difference between lower() and casefold()
lower() is sufficient for everyday English case conversion, but casefold() normalizes a broader range of Unicode characters — for example, the German ß (Eszett) becomes ss. The difference matters in internationalized string comparisons.
mistake2_ng.py
s = "Straße" print(s.lower()) print(s.lower() == "strasse")
Running the code produces the following output:
python3 mistake2_ng.py straße False
mistake2_ok.py
s = "Straße" print(s.casefold()) print(s.casefold() == "strasse")
Running the code produces the following output:
python3 mistake2_ok.py strasse True
Notes
All of these methods return a new string. The original string is not modified (strings are immutable). Case conversion only affects ASCII letters — digits, symbols, and non-Latin characters are not changed.
To compare strings in a case-insensitive way, the common approach is to convert both strings to lowercase with lower() before comparing. For stricter comparisons involving special characters such as the German 'ß', the casefold() method normalizes a broader range of Unicode uppercase and lowercase characters. casefold() applies more aggressive lowercasing than lower() and is better suited for internationalized comparisons.
For string formatting (such as number formatting or padding), see 'str.format() / f-strings'. To check whether a string starts with a specific pattern, see 'str.startswith() / str.endswith()'.
If you find any errors or copyright issues, please contact us.