While working with String lists, the problem of cases is common, but sometimes, we are concerned about changes cases in strings selectively. i.e on basis of other list. This can have application in day-day programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + upper() + enumerate()
This is one of the ways in which this task can be performed. In this we run a loop for each element and the compare strings, if found equal, then cases of those lists don’t change and rest strings cases are changed.
# Python3 code to demonstrate # Specific case change in String List # using loop + upper() + enumerate() # Initializing lists test_list1 = ['GFG', 'IS', 'BEST', 'FOR', 'GEEKS'] test_list2 = ['Gfg', 'Best'] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Specific case change in String List # using loop + upper() + enumerate() for idx, ele in enumerate(test_list1): for ele2 in test_list2: if ele == ele2.upper(): test_list1[idx] = ele2 # printing result print ("The string list after case change is : " + str(test_list1)) |
The original list 1 is : ['GFG', 'IS', 'BEST', 'FOR', 'GEEKS'] The original list 2 is : ['Gfg', 'Best'] The string list after case change is : ['Gfg', 'IS', 'Best', 'FOR', 'GEEKS']
Method #2 : Using loop + capitalize()
This method performs similar to above one, the difference being that instead of upper(), capitalize() is used to perform the task of changing cases.
# Python3 code to demonstrate # Specific case change in String List # using loop + capitalize() # Initializing lists test_list1 = ['GFG', 'IS', 'BEST', 'FOR', 'GEEKS'] test_list2 = ['Gfg', 'Best'] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Specific case change in String List # using loop + capitalize() for idx, ele in enumerate(test_list1): if ele.capitalize() in test_list2: test_list1[idx] = ele.capitalize() # printing result print ("The string list after case change is : " + str(test_list1)) |
The original list 1 is : ['GFG', 'IS', 'BEST', 'FOR', 'GEEKS'] The original list 2 is : ['Gfg', 'Best'] The string list after case change is : ['Gfg', 'IS', 'Best', 'FOR', 'GEEKS']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python program to convert camel case string to snake case
- Python - Convert Snake Case String to Camel Case
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python - Convert Snake case to Pascal case
- Python - Change Keys Case in Dictionary
- PyQt5 - Change the item at specific index in ComboBox
- Python - Specific Characters Frequency in String List
- Python | Convert List of String List to String List
- Python | Index specific cyclic iteration in list
- Python | Sort list of tuples by specific ordering
- Python | Find the list elements starting with specific letter
- Python | Remove element from given list containing specific digits
- Python - Specific Range Addition in List
- Python - Binary operation on specific keys in Dictionary List
- Python program to extract only the numbers from a list which have some specific digits
- Python | Convert case of elements in a list of strings
- Python | Ways to sort list of strings in case-insensitive manner
- Python | Filter String with substring at specific position
- Python | Filter String with substring at specific position
- Python - Replace vowels in a string with a specific character K
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.

