Convert a list of strings to upper case or lower case using python

In this article, we shall see how to convert a list of strings from upper to lower case or from lower to upper case using python. This can be achieved either using list comprehension or using map method and lambda function.

Let us get started.

Convert to lower case

Consider the following list of strings. These strings are in upper case.

strings = ['PYTHON', 'JAVA', 'C', 'PHP', 'JAVASCRIPT']

First, let us convert this list of strings to lower case using list comprehension. We will be using the lower() method to convert these strings to lower case.

python string lower case conversion

The output of the above code is,

['python', 'java', 'c', 'php', 'javascript']

Next, we shall see how to convert this list of strings to lowercase using the map() method and a lambda expression. The map() methods take a lambda expression which will have the lower() method and the iterable as its arguments. The list of strings is our iterable.

string conversion using map method and lambda expression

The output of the above code is,

['python', 'java', 'c', 'php', 'javascript']

Convert to upper case

Similar to the above code we can easily convert the list of lowercase strings to upper case strings using the upper() method.

Consider the following list of lowercase characters.

['python', 'java', 'c', 'php', 'javascript']

First let us convert this using list comprehension. Since only the method will change from lower() to upper() here, I am not explaining them much.

python string conversion to upper case

The output of this code is,

['PYTHON', 'JAVA', 'C', 'PHP', 'JAVASCRIPT']

Similarly using map() method and lambda expression we can write it as,

string upper case conversion using map and lambda function

The output of this code is,

['PYTHON', 'JAVA', 'C', 'PHP', 'JAVASCRIPT']

Conclusion

Hope this article is helpful. If you have any queries leave them in the comments below. I will try to answer them as quickly as possible.

Happy coding.