Skip to main content

How to convert a list of strings to lowercase in Python

How to convert a list of strings to lowercase in Python.

Here's a step-by-step tutorial on how to convert a list of strings to lowercase in Python.

Step 1: Create a list of strings

Start by creating a list of strings that you want to convert to lowercase. For example, let's create a list called my_list with some strings:

my_list = ["HELLO", "WORLD", "PYTHON"]

Step 2: Use a for loop to iterate through the list

To convert each string in the list to lowercase, you can use a for loop to iterate through each element. The loop will assign each string to a temporary variable, and then you can modify it. Here's an example:

for i in range(len(my_list)):
my_list[i] = my_list[i].lower()

In this example, len(my_list) returns the length of the list, and range(len(my_list)) generates a sequence of numbers from 0 to the length of the list minus 1. The for loop then iterates over this sequence.

Step 3: Convert each string to lowercase

Inside the loop, you can use the lower() method to convert each string to lowercase. The lower() method returns a new string with all uppercase characters converted to lowercase. By assigning the modified string back to the original list element, you update the element with the lowercase version. Here's the updated code:

for i in range(len(my_list)):
my_list[i] = my_list[i].lower()

After executing this loop, the my_list will be updated with the lowercase versions of the strings.

Step 4: Print the modified list

Finally, you can print the modified list to verify that the conversion to lowercase was successful. Here's an example:

print(my_list)

When you run this code, it will output:

['hello', 'world', 'python']

This confirms that the strings in the list have been successfully converted to lowercase.

Alternate Approach: List comprehension Python also provides a more concise way to achieve the same result using list comprehension. Here's an example:

my_list = [x.lower() for x in my_list]

In this approach, the lower() method is applied to each element x in the original list, and the resulting lowercase strings are collected in a new list. This new list is then assigned back to the variable my_list, effectively replacing the original list with the lowercase version.

That's it! You now know how to convert a list of strings to lowercase in Python.