Python | Convert string list into multiple cases
Sometimes, while working with Python strings, we might have a problem in which we have list of strings and we wish convert them into specified cases. This problem generally occurs in the cases in which the strings we recieve are ill-cased. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + inbuilt functions
In this method, we use list comprehension as shortened way to perform this task rather than loop method that could span some lines of codes. The conversions are made using generic inbuilt functions that can perform interconversion tasks.
# Python3 code to demonstrate working of # Convert string list into multiple cases # Using inbuilt functions + list comprehension # Initializing list test_list = ['bLue', 'ReD', 'yeLLoW'] # printing original list print("The original list is : " + str(test_list)) # Convert string list into multiple cases # Using inbuilt functions + list comprehension res = [(ele.upper(), ele.title(), ele.lower()) for ele in test_list] # printing result print("The list with multiple cases are : " + str(res)) |
The original list is : ['bLue', 'ReD', 'yeLLoW']
The list with multiple cases are : [('BLUE', 'Blue', 'blue'), ('RED', 'Red', 'red'), ('YELLOW', 'Yellow', 'yellow')]
Method #2 : Using map() + lambda + inbuilt functions
This is another method to perform this particular task. In this, we just perform the task of extending the logic of conversions using lambda and iterations and application to each string is done by lambda function.
# Python3 code to demonstrate working of # Convert string list into multiple cases # Using map() + lambda + inbuilt functions # Initializing list test_list = ['bLue', 'ReD', 'yeLLoW'] # printing original list print("The original list is : " + str(test_list)) # Convert string list into multiple cases # Using map() + lambda + inbuilt functions res = list(map(lambda ele: (ele.upper(), ele.title(), ele.lower()), test_list)) # printing result print("The list with multiple cases are : " + str(res)) |
The original list is : ['bLue', 'ReD', 'yeLLoW']
The list with multiple cases are : [('BLUE', 'Blue', 'blue'), ('RED', 'Red', 'red'), ('YELLOW', 'Yellow', 'yellow')]
Recommended Posts:
- Python | Convert a list of multiple integers into a single integer
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string enclosed list to list
- Python | Convert a string representation of list into list
- Python | Convert list of string to list of list
- Python | Convert String to list of tuples
- Python program to convert a list to string
- Python | Program to convert String to a List
- Python | Convert a list of characters into a string
- Python | Ways to convert list of ASCII value to string
- Python | Convert list of strings to space separated string
- Python | Convert string tuples to list tuples
- Python | Convert list of tuples to list of list
- Python | Convert list of tuples to list of strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



