How to Change Python String Cases

To convert a Python string to lowercase, use the built-in lower() method of a string. To convert a Python string to uppercase, use the built-in upper() method.

Here is an example:

"Hello, world".upper() # HELLO WORLD
"HELLO, WORLD".lower() # hello world

Let’s see other case conversions in Python.

How to Check If a String Is in Lowercase/Uppercase

If you want to test if a string is in uppercase or lowercase in Python, use the built-in isupper() and islower() methods:

For example:

"Hello, world".islower() # False
"HELLO, WORLD".isupper() # True

How to Capitalize the First Letter of a String in Python

To capitalize the first letter of a string in Python, use the built-in capitalize() method.

Here is an example:

"hello, world".capitalize() # Hello, world

How to Swap Cases in Python

To convert each lowercase letter to uppercase and each uppercase letter to lowercase, use the swapcase() method.

For instance:

"HELLO, world".swapcase() # hello, WORLD

How to Title Case a String in Python

Title case refers to a string where the first letter of each word is capitalized.

To capitalize the first letter of each word in a string, use the title case converter by calling title() method.

For example:

"hello, world".title() # Hello, World

Conclusion

Today you learned how to convert strings to lowercase and to uppercase in Python. Also, you saw some examples of how to apply other casings too.

Make sure you check out other useful string methods in Python.

Thanks for reading. I hope you find it useful.

Happy coding!

Further Reading

50 Python Interview Questions and Answers

50+ Buzzwords of Web Development

Leave a Comment

Your email address will not be published.