Convert case of elements in a list of strings in Python


As part of data manipulation, we will come across the need to have a single case for all the letters in a string. In this article we will see how to take a list which has string elements with mixed cases. We then apply some python functions to convert them all to a single case.

With lower()

The lower function is a string function that can convert the entire string to lower case. So we use lambda and map to apply the lower function to each of the elements in the list.

Example

 Live Demo

listA = ['MoN', 'TuE', 'FRI']
# Given list
print("Given list : \n",listA)
res = list(map(lambda x: x.lower(), listA ))
# printing output
print("New all lowercase list: \n",res)

Output

Running the above code gives us the following result −

Given list :
['MoN', 'TuE', 'FRI']
New all lowercase list:
['mon', 'tue', 'fri']

with upper()

In this approach we directly apply the upper() to the list through a for loop. So each string gets converted to uppercase letters.

Example

 Live Demo

listA = ['MoN', 'TuE', 'FRI']
# Given list
print("Given list : \n",listA)
res = [x.upper() for x in listA]
# printing output
print("New all uppercase list: \n",res)

Output

Running the above code gives us the following result −

Given list :
['MoN', 'TuE', 'FRI']
New all uppercase list:
['MON', 'TUE', 'FRI']

Updated on: 20-May-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements