How to lowercase a String in Python?

How to lowercase a String in Python?

Answer: You can use the lower() method to lowercase a string in Python.

Lowercasing a string in Python is a common operation. Python provides various built-in methods to convert all characters of a string to lowercase. In this blog, we’ll explore how to use these predefined methods along with examples.

Table of Contents:

Methods to Lowercase a String in Python

Python provides various built-in methods to convert characters of a string to lowercase. Here, we have explained these methods based on their usage:

Method 1: Using the lower() Method to lowercase a String in Python 

The lower() method is commonly used when working with text data, especially in cases when case consistency is important. It is often applied in tasks like data cleaning or natural language processing, where it’s useful to convert all text to lowercase.

Example 1: Simple Use-case of lower() method

Python

The lower() method converts the uppercase ‘H’ and ‘I’ to lowercase.

Example 2: Comparison of case-sensitive strings using lower()

Python

Output: 

Here, lower() makes the comparison by converting both strings to lowercase.  

Note: The return type of lower() method is “string”

Example 3: Removing case sensitivity using lower()

Python

Output: 

Method 2: Using the casefold() Method to lowercase a String in Python 

It is similar to the lower() method but is more aggressive, the casefold() method converts a string to lowercase including the ASCII characters too, which was a limitation with a lower() method.

Example:

Python

Output: 

Difference between casefold() and lower() in Python 

The lower() method in Python converts all characters in a string to lowercase but it doesn’t handle special characters like (Ä or ß). Whereas the casefold() method also converts the characters to lowercase but this method is defined for case-insensitive comparisons and also for handling special characters.

Example: 

Python

Output: 

Method 3: Using a Loop with ord() and chr() to lowercase a String in Python

By using ord() you can manually convert each character from uppercase to lowercase by checking its ASCII value and by using chr() you can adjust the corresponding lowercase character. 

Example: 

Python

Output:

Conclusion

The .lower() method in Python converts all characters of a string to lowercase, leaving non-alphabetic characters unchanged. It is useful for text normalization, case-insensitive comparisons, and data cleaning. The method returns a new string, and the original string remains unchanged. It’s commonly used in tasks like user input validation, search, and natural language processing.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner