Case Conversion Methods in Python
Python provides case conversion methods that allow us to change the case of characters in a string. These methods help us convert text to uppercase, lowercase, capitalize the first letter, swap cases, etc. These are non-destructive methods, meaning they return a new string and do not change the original one.
1. upper() Method
The upper() method is used to convert all the characters in a string to
uppercase.
It returns a new string where all lowercase alphabets are changed to their uppercase form, while
other characters (like numbers or symbols) remain unchanged.
Syntax:
string.upper()
Example:
text = "hello"
print(text.upper())
print(text)
Output:
HELLO
hello
Explanation:
- The original string is
"hello". - The
upper()method converts all characters to uppercase:"HELLO". - These methods are non-destructive, meaning they
do not change the
original string. So,
print(text)will still output"hello", not the uppercase version.
2. lower() Method
The lower() method is used to convert all the characters in a string to
lowercase.
It returns a new string in which all uppercase alphabets are converted to their lowercase form.
Characters like numbers and special symbols remain unchanged.
Syntax:
string.lower()
Example:
text = "HELLO"
print(text.lower())
Output:
hello
Explanation:
- The original string is
"HELLO". - The
lower()method converts it to all lowercase:"hello".
3. capitalize() Method
The capitalize() method converts the first character of the string to
uppercase, and all the remaining characters to lowercase.
It is commonly used to make the string appear like a sentence, starting with a capital letter.
Syntax:
string.capitalize()
Example:
text = "hELLo world"
print(text.capitalize())
Output:
Hello world
Explanation:
- The first character
'h'becomes uppercase. - The rest of the characters are converted to lowercase.
- Final result:
"Hello world"
4. title() Method
The title() method converts the first character of each word in the
string to uppercase, and the rest of the characters to lowercase.
This is useful when formatting strings in a title-like or heading format.
Syntax:
string.title()
Example:
text = "hello python world"
print(text.title())
Output:
Hello Python World
Explanation:
- Every word in the string starts with an uppercase character.
- Words are detected by space separation.
- Final result:
"Hello Python World"
5. swapcase() Method
The swapcase() method inverts the case of each character in the string
—
characters in uppercase become lowercase, and those in lowercase become
uppercase.
This is helpful when you want to flip the case pattern of a string.
Syntax:
string.swapcase()
Example:
text = "Hello PYthon"
print(text.swapcase())
Output:
hELLO pyTHON
Explanation:
'H'becomes'h','e'becomes'E', and so on.- All cases are swapped accordingly.
String Case Conversion Methods - Summary Table
| Method | Short Description |
|---|---|
| upper() | Converts all characters of the string to uppercase. |
| lower() | Converts all characters of the string to lowercase. |
| capitalize() | Converts the first character to uppercase and the rest to lowercase. |
| title() | Makes the first letter of each word uppercase. |
| swapcase() | Flips the case of each character – upper becomes lower and vice versa. |