Convert the Case of Elements in a List of Strings in PythonWrite a Python program that converts all strings from lowercase/uppercase to uppercase/lowercase given a list of strings. Method 1: Use the map function to convert uppercase to lowercase Program Explanation: The Python method converts all uppercase to lowercase strings in a specified list using the map function and a lambda function. Every member of the input list is subjected to the lambda function lambda x: x.lower() via the map, which converts the strings to lowercase. Once the result has been transformed into a list, the output, which comprises the lowercase versions of the input strings, is printed. When the program runs, its final output will be ['java', 't', 'point']. Output: ['java', 't', 'point']
Method 2: Use list comprehension to change lowercase to uppercase Program Explanation: The given Python function initializes an input list of mixed-case word strings. Iterating through each element in the input list using a list comprehension, it uses the upper() function to change each string to uppercase. Every string gets capitalized in the final list list. When the program is run, it will display ['FUN,' 'FOO,' 'BAR'], demonstrating the conversion of all strings from lowercase to uppercase. Finally, the program outputs the output. Output: ['FUN', 'FOO', 'BAR']
Method 3: Using enumerate function Program Explanation: The provided Python code initialises a list called lst that has strings of different case. It iterates through each element and its matching index in the original list using list comprehension and the enumerate function. It uses the lower() function to change each string i to lowercase. The lowercase versions of the original strings are now stored in the modified list lst. The code outputs the output after it runs, which is ['apple,' 'apple,' 'java'], illustrating how all strings are converted to lowercase. Output: ['apple', 'apple', 'java']
Method 4: Using case fold() The str. casefold() function is used as the technique. This function converts the string to a lowercase form more aggressively than the str. lower() method does. It is very helpful when managing case conversions involving non-ASCII characters, such as those found in German. This is an illustration of how to transform a list of strings to lowercase using str. case fold(): Program Explanation: Mixed-case strings are used to initialize a list input in the supplied Python function. In order to ensure case-insensitive comparison, it iterates through each string in the input list using a list comprehension. It uses the casefold() function to transform each text to its casefolded version. All of the strings are present in the output list in their casefolded format. Upon program execution, the result is printed as ['java,' 't,' 'point'], demonstrating the case-insensitive conversion of all strings to lowercase. Output: ['java', 't', 'point']
Method 5: Using the lower() Program Explanation: The above Python code creates an initialised list of input strings that have mixed case. Iterating over each element in the input list, it uses a list comprehension to apply the lower() function to each string, changing it to lowercase. The lowercase equivalents of the original strings are contained in the list lst that is produced. The output of the program, which produces ['java,' 't,' 'point'] after it runs, shows that all strings have been converted to lowercase. Output: ['java', 't', 'point']
Method 6: Using a for loop Transform uppercase letters to lowercase by iterating through each character in the string using a for loop and then converting the uppercase letter to lowercase using the lower() method. Program Explanation: The for loop used in the Python code provides a function that converts all capital characters in a specified text string to lowercase. It uses the isupper() function to determine if each character in the string is uppercase before iterating over each character and using the lower() method to change it to lowercase. The character doesn't change if it isn't capitalized. The variable output contains the modified string. When the code runs, it outputs the changed text, which becomes "java t point," showing that all capital letters have been converted to lowercase. Output: java t point
Next TopicPython all |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India