How to Change String Cases in Python
Table of Contents
- How to Convert a String to Uppercase in Python
- How to Convert a String to Lowercase in Python
- How to Check if a String is Uppercase in Python
- How to Check if a String is Lowercase in Python
- How to Capitalize the First Letter of a String in Python
- How to Swap Case of a String in Python
- How to Title Case a String in Python
- Conclusion
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:
PYTHONstring = "Hello World"
Now let's convert it to uppercase:
PYTHONstring = "Hello World"
string.upper()
print(string)
BASHHELLO 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:
PYTHONstring = "Hello World"
string.lower()
print(string)
BASHhello 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:
PYTHONstring = "Hello World"
string.isupper()
print(string)
BASHFalse
Here's another example, this time the result will be True
:
PYTHONstring = "HELLO WORLD"
string.isupper()
print(string)
BASHTrue
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:
PYTHONstring = "Hello World"
string.islower()
print(string)
BASHFalse
Here's another example, this time the result will be True
:
PYTHONstring = "hello world"
string.islower()
print(string)
BASHTrue
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:
PYTHONstring = "hello world"
string.capitalize()
print(string)
BASHHello 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:
PYTHONstring = "Hello World"
string.swapcase()
print(string)
BASHhELLO 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:
PYTHONstring = "hello world"
string.title()
print(string)
BASHHello 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!
- How to Install Node on Windows, macOS and Linux
- Getting Started with Solid
- Managing PHP Dependencies with Composer
- How to Set Up Cron Jobs in Linux
- Best Visual Studio Code Extensions for 2022
- How to build a Discord bot using TypeScript
- Getting Started with Sass
- Learn how to use v-model with a custom Vue component
- Getting User Location using JavaScript's Geolocation API
- Learn how to build a Slack Bot using Node.js
- Creating a Twitter bot with Node.js
- Building a Real-Time Note-Taking App with Vue and Firebase