Converting uppercase and lowercase letters is a common task, often used to standardize inconsistent user inputs for email addresses or to format article titles.
Python’s string type (str) provides several useful methods for performing these conversions.
In this article, I will explain the basic upper() and lower() methods, as well as capitalize() and title(), which handle sentence beginnings and word-by-word formatting.
Table of Contents
- Make everything uppercase or lowercase:
upper(),lower() - Capitalize only the beginning:
capitalize() - Capitalize the beginning of each word:
title() - Invert uppercase and lowercase:
swapcase() - Summary
1. Make everything uppercase or lowercase: upper(), lower()
These methods are the most frequently used for standardizing entire strings.
upper(): Converts all characters to uppercase.lower(): Converts all characters to lowercase.
These are often used for normalization when comparing alphabets (e.g., treating “Yes” and “yes” as the same).
Specific Use Case: Code Normalization
Here is an example of converting a product code mixed with uppercase and lowercase into a unified format.
# User input (inconsistent formatting)
input_code = "Item-a001-XyZ"
# Convert all to uppercase
upper_code = input_code.upper()
# Convert all to lowercase
lower_code = input_code.lower()
print(f"Original : {input_code}")
print(f"Uppercase: {upper_code}")
print(f"Lowercase: {lower_code}")
Execution Result:
Original : Item-a001-XyZ
Uppercase: ITEM-A001-XYZ
Lowercase: item-a001-xyz
Note: In Python 3, full-width alphabets (e.g., ABC) are also subject to conversion.
2. Capitalize only the beginning: capitalize()
The capitalize() method makes only the first character of the string uppercase and converts all other characters to lowercase.
It is suitable for formatting the start of sentences.
# Sentence with lowercase start and uppercase in the middle
sentence = "python is FUN."
# Only the start is uppercase, the rest becomes lowercase
formatted = sentence.capitalize()
print(f"Before: {sentence}")
print(f"After : {formatted}")
Execution Result:
Before: python is FUN.
After : Python is fun.
Notice that FUN in the middle has been lowercased to fun.
3. Capitalize the beginning of each word: title()
The title() method converts the first character of every word in the string to uppercase and the rest to lowercase (Title Case).
It is used for Western-style titles or name formatting.
# Book title (lowercase only)
book_title = "the hitchhiker's guide to the galaxy"
# Capitalize the beginning of each word
title_cased = book_title.title()
print(f"Before: {book_title}")
print(f"After : {title_cased}")
Execution Result:
Before: the hitchhiker's guide to the galaxy
After : The Hitchhiker'S Guide To The Galaxy
Note: title() has a characteristic where it treats characters after apostrophes as “the start of a new word” and capitalizes them (e.g., Hitchhiker'S). If strict title casing is required, it is recommended to use the capwords() function from the string module.
4. Invert uppercase and lowercase: swapcase()
The swapcase() method flips uppercase to lowercase and lowercase to uppercase.
text = "Hello Python"
swapped = text.swapcase()
print(swapped)
Execution Result:
hELLO pYTHON
Summary
upper(): Makes the whole string uppercase.lower(): Makes the whole string lowercase.capitalize(): Makes only the first character uppercase and the rest lowercase.title(): Makes the beginning of each word uppercase.
By selecting the appropriate method for your purpose, you can efficiently format text data.
