How to Change String Cases in Python

Updated onbyAlan Morel
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:

PYTHON
string = "Hello World"

Now let's convert it to uppercase:

PYTHON
string = "Hello World" string.upper() print(string)
BASH
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:

PYTHON
string = "Hello World" string.lower() print(string)
BASH
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:

PYTHON
string = "Hello World" string.isupper() print(string)
BASH
False

Here's another example, this time the result will be True:

PYTHON
string = "HELLO WORLD" string.isupper() print(string)
BASH
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:

PYTHON
string = "Hello World" string.islower() print(string)
BASH
False

Here's another example, this time the result will be True:

PYTHON
string = "hello world" string.islower() print(string)
BASH
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:

PYTHON
string = "hello world" string.capitalize() print(string)
BASH
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:

PYTHON
string = "Hello World" string.swapcase() print(string)
BASH
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:

PYTHON
string = "hello world" string.title() print(string)
BASH
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!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.