Table of contents
1.
Introduction
2.
str.lower() Method
2.1.
Syntax 
2.2.
Parameters
2.3.
Example
2.4.
Python
3.
How to use the Python string lower() Method?
4.
String with Only Alphabetic Characters
4.1.
Python
5.
Lower() Function to Convert String to Lower Case
5.1.
Python
6.
Comparison of Strings Using lower() Method
7.
Function to Convert String to Lower Case
7.1.
Python
8.
Other Methods to Convert String to Lower Case
9.
Applications of String lower() method
10.
casefold() Function to Convert String to Lower Case
10.1.
Python
11.
When to Use Which Method
12.
Frequently Asked Questions
12.1.
How do you lowercase in Python 2?
12.2.
How do you make all characters lowercase in Python?
12.3.
What is the difference between lower() and casefold() in Python?
13.
Conclusion
Last Updated: Sep 27, 2024
Easy

Python Lowercase

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, the `lower()` method is used to convert a string to lowercase. It returns a new string where all the characters are converted to their lowercase equivalents. This method is useful for case-insensitive comparisons, text processing, and normalization of string data. The `lower()` method does not modify the original string; instead, it returns a new string with the lowercase characters. In this article, we will discuss these methods, their syntax, and provide examples with complete code examples.

python lowercase.

str.lower() Method

The `str.lower()` method in Python is used to convert all the characters in a string to lowercase. It returns a new string with the lowercase characters, leaving the original string unchanged. This method is useful for case-insensitive comparisons and text-processing tasks where the case of the characters is not important.

Syntax 

str.lower()

Parameters

This method does not take any parameters.

Example

Convert a String to Lowercase

# Python program to demonstrate the use of str.lower() method

  • Python

Python

# Original string

original_str = "PYTHON IS FUN"

# Converting to lowercase

lowercase_str = original_str.lower()

print(lowercase_str) 
You can also try this code with Online Python Compiler
Run Code

Output: 

python is fun

In this example, the lower() method converts all uppercase letters in the string original_str to lowercase, and the result is stored in lowercase_str.

How to use the Python string lower() Method?

To use the `lower()` method in Python, you simply need to call it on a string object. Here's how you can use it:

string_name.lower()
You can also try this code with Online Python Compiler
Run Code

Or, you can assign the result to a new variable:

new_string = string_name.lower()
You can also try this code with Online Python Compiler
Run Code

 

Let's see a few examples to understand the usage of `lower()`:

Example 1:

text = "Hello, World!"
lowercase_text = text.lower()
print(lowercase_text)
You can also try this code with Online Python Compiler
Run Code


Output:

hello, world!
You can also try this code with Online Python Compiler
Run Code

 

Example 2:

message = "PyTHoN iS AWesoME!"
normalized_message = message.lower()
print(normalized_message)

You can also try this code with Online Python Compiler
Run Code

Output:
 

python is awesome!
You can also try this code with Online Python Compiler
Run Code

 

In the first example, the `lower()` method is called on the string `"Hello, World!"`, and the resulting lowercased string is assigned to the variable `lowercase_text`. The lowercased string is then printed.

In the second example, the `lower()` method is called on the string `"PyTHoN iS AWesoME!"`, which contains mixed case characters. The resulting lowercased string is assigned to the variable `normalized_message`, and then it is printed.

Remember that the `lower()` method does not modify the original string; it returns a new string with the lowercased characters. If you want to update the original string, you need to assign the result back to the original variable:

text = "Hello, World!"
text = text.lower()
print(text)
You can also try this code with Online Python Compiler
Run Code

 

Output:

hello, world!

The `lower()` method is particularly useful when you want to perform case-insensitive comparisons or when you need to normalize the case of strings for consistency. It is a common operation in text processing and string manipulation tasks.

String with Only Alphabetic Characters

# Python program to convert a string with only alphabetic characters to lowercase

  • Python

Python

# Original string

original_str = "HELLO WORLD"

# Converting to lowercase

lowercase_str = original_str.lower()

print(lowercase_str) 
You can also try this code with Online Python Compiler
Run Code

Output: 

hello world


The lower() method works perfectly with strings containing only alphabetic characters.

Lower() Function to Convert String to Lower Case

The lower() method is not a separate function but a method of string objects in Python. Here's how it's used with a string containing alphanumeric characters:

String with Alphanumeric Characters

# Python program to convert a string with alphanumeric characters to lowercase

  • Python

Python

# Original string

original_str = "Python3.8"

# Converting to lowercase

lowercase_str = original_str.lower()

print(lowercase_str) 
You can also try this code with Online Python Compiler
Run Code

 Output:

python3.8


The numeric and special characters remain unaffected, as lower() only changes alphabetic characters.

Also see, Python Operator Precedence

Comparison of Strings Using lower() Method

Comparing strings in a case-insensitive manner often involves converting both strings to lowercase before the comparison.

# Python program to compare strings using lower() method

# Two strings

str1 = "Python"
str2 = "python"

# Case-insensitive comparison

if str1.lower() == str2.lower():
    print("The strings are the same.")
else:
    print("The strings are different.")
swapcase() 

Function to Convert String to Lower Case

The swapcase() method is used to swap the case of each letter in the string. Uppercase becomes lowercase and vice versa.

# Python program to demonstrate the use of swapcase() method

  • Python

Python

# Original string

original_str = "PyThOn"

# Swapping case

swapped_str = original_str.swapcase()

print(swapped_str) 
You can also try this code with Online Python Compiler
Run Code

Output: 

pYtHoN

Other Methods to Convert String to Lower Case

In Python, there are a few other methods and techniques you can use to convert a string to lowercase besides the `lower()` method. Now, lets discuss a few alternative approaches:

1. `str.casefold()` method:

  • The `casefold()` method is similar to `lower()`, but it provides a more aggressive lowercasing transformation.
  • It is mainly used for case-insensitive comparisons and is particularly useful when dealing with Unicode strings.
  • Example:

 

text = "ẞ is a German letter."
lowercased_text = text.casefold()
print(lowercased_text)    
You can also try this code with Online Python Compiler
Run Code


Output:

ss is a german letter.    

2. Using `str.translate()` with a custom translation table:

  • The `translate()` method allows you to perform character translations based on a translation table.
  • You can create a custom translation table using the `str.maketrans()` method to map uppercase characters to their lowercase equivalents.
  • Example:
     
text = "Hello, World!"
translation_table = str.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
lowercased_text = text.translate(translation_table)
print(lowercased_text)   
You can also try this code with Online Python Compiler
Run Code

Output:
 

hello, world!   

3. Using list comprehension and `str.join()`:

  • You can use a list comprehension to iterate over each character in the string and convert it to lowercase using the `lower()` method.
  • Then, you can join the lowercase characters back into a string using the `join()` method.
  • Example:
     
 text = "Hello, World!"
 lowercased_text = ''.join([char.lower() for char in text])
 print(lowercased_text)
You can also try this code with Online Python Compiler
Run Code


Output:  

hello, world!   

Applications of String lower() method

 

1. Case-insensitive comparisons: The `lower()` method is commonly used when performing case-insensitive comparisons between strings. By converting both strings to lowercase before comparing them, you can determine if they are equal regardless of their original casing. This is particularly useful when dealing with user input or text data where the case may vary.

2. Text normalization: In text processing and analysis tasks, it's often necessary to normalize the text data to a consistent format. Converting all the text to lowercase using `lower()` is a common preprocessing step. It helps standardize the text and makes it easier to perform operations like searching, matching, or aggregating data.

3. String matching and searching: When searching for specific patterns or substrings within a larger text, using `lower()` can make the search case insensitive. By converting both the search pattern and the text to lowercase, you can find matches regardless of the original casing. This is handy when implementing search functionality or text filtering.

4. Data cleaning and preparation: In data cleaning and preparation tasks, the `lower()` method is often used to standardize and clean text data. It helps address inconsistencies in casing and makes the data more uniform. For example, when working with user-generated content or data from different sources, using `lower()` can help normalize the text before further processing or analysis.

5. Sorting and indexing: When sorting strings or building indexes based on text data, using `lower()` can provide a case-insensitive ordering. By converting the strings to lowercase before sorting or indexing, you can ensure that the order is based on the content of the strings rather than their casing. This is useful in scenarios where the case of the text is not significant for sorting purposes.

6. String formatting and display: In some cases, you may want to display text in a consistent lowercase format for aesthetic or readability purposes. Using `lower()` allows you to format strings in lowercase before displaying them to the user or including them in output files or reports.

casefold() Function to Convert String to Lower Case

The casefold() method is similar to lower(), but it is more aggressive and is used for caseless matching. It removes all case distinctions in the string.

# Python program to demonstrate the use of casefold() method

  • Python

Python

# Original string

original_str = "Straße"

# Converting to casefold

casefold_str = original_str.casefold()

print(casefold_str) 
You can also try this code with Online Python Compiler
Run Code

Output: 

strasse


The German letter 'ß' is converted to 'ss', showing casefold()'s effectiveness for internationalization.

When to Use Which Method

Here's a table summarizing when to use each method:

Method  Use Case
lower()  Standard lowercase conversion
swapcase()   Inverting the case of each letter
casefold()   Aggressive lowercase conversion for caseless matching

Frequently Asked Questions

How do you lowercase in Python 2?

In Python 2, use the .lower() method on a string to convert all characters to lowercase: string.lower().

How do you make all characters lowercase in Python?

In Python, apply the .lower() method to a string to convert all uppercase characters to lowercase: myString.lower().

What is the difference between lower() and casefold() in Python?

The lower() method converts letters to lowercase based on standard conventions, while casefold() is more aggressive and handles more diverse characters for better case-insensitive matching.

Conclusion

In Python, converting strings to lowercase is a common operation that can be achieved using methods like lower(), swapcase(), and casefold(). Each method serves a specific purpose, from simple case conversion to complex caseless matching. Understanding these methods allows for more effective string manipulation and prepares you to handle a variety of text processing tasks. By following the examples provided, you can confidently apply these methods in your Python programs.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass