How to Change String Cases in Python

When you use Python, you are likely working with strings all the time.
One of the most common operations you will perform on strings is to convert them to uppercase or lowercase, check if they are uppercase or lowercase, or to capitalize them.
In this post, we will learn how to do all of these things in Python.
How to Convert a String to Uppercase in Python
To convert a string to uppercase in Python, you can use the upper()
method.
The upper()
method returns a copy of the string with all the characters in uppercase.
First let's define a string:
string = "Hello World"
Now let's convert it to uppercase:
string = "Hello World"
string.upper()
print(string)
HELLO WORLD
How to Convert a String to Lowercase in Python
Similarly, to convert a string to lowercase in Python, you can use the lower()
method.
The lower()
method returns a copy of the string with all the characters in lowercase.
Let's convert the string we defined above to lowercase:
string = "Hello World"
string.lower()
print(string)
hello world
How to Check if a String is Uppercase in Python
To check if a string is uppercase in Python, you can use the isupper()
method.
This method returns True
if all the characters in the string are uppercase, otherwise it returns False
.
Let's check if the string we defined above is uppercase:
string = "Hello World"
string.isupper()
print(string)
False
Here's another example, this time the result will be True
:
string = "HELLO WORLD"
string.isupper()
print(string)
True
How to Check if a String is Lowercase in Python
Just like with the isupper()
method, you can use the islower()
method to check if a string is lowercase.
This method returns True
if all the characters in the string are lowercase, otherwise it returns False
.
Let's check if the string we defined above is lowercase:
string = "Hello World"
string.islower()
print(string)
False
Here's another example, this time the result will be True
:
string = "hello world"
string.islower()
print(string)
True
How to Capitalize the First Letter of a String in Python
To capitalize the first letter of a string in Python, you can use the capitalize()
method.
This method returns a copy of the string with only the first character capitalized.
Let's capitalize the first letter of the string we defined above:
string = "hello world"
string.capitalize()
print(string)
Hello world
How to Swap Case of a String in Python
This is rarely needed, but Python has a built-in method to swap the case of a string.
This means that all lowercase characters will be converted to uppercase, and all uppercase characters will be converted to lowercase.
To swap the case of a string in Python, you can use the swapcase()
method.
Let's swap the case of the string we defined above:
string = "Hello World"
string.swapcase()
print(string)
hELLO wORLD
How to Title Case a String in Python
Finally, Python also has a built-in method to convert a string to title case.
Title case is when the first letter of each word is capitalized.
This can be useful when you want to display a string in a title of an article post, for example.
To convert a string to title case in Python, you can use the title()
method.
Let's convert the string we defined above to title case:
string = "hello world"
string.title()
print(string)
Hello World
Conclusion
In this post, we learned how to convert a string to uppercase or lowercase, check if a string is uppercase or lowercase, capitalize the first letter of a string, swap the case of a string, and convert a string to title case in Python.
Thanks for reading!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on X! You can also join the conversation over at our official Discord!
Leave us a message!