Convert the Case of Elements in a List of Strings in Python5 Jan 2025 | 5 min read Write 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 |
An Introduction to Plot Axis Spines The boundaries or margins of a plot that enclose the data region are referred to as spines in the Matplotlib library. These spines encircle the plot's edges, delineating the region in which the data points are shown. A plot has...
9 min read
Indeed, there are several methods you can use to make a function work its best, whether you want to make it as small as possible or as large as possible. Various optimization techniques are available online, but none is a one-size-fits-all solution. Each method has...
16 min read
Python is an interpreted high-level language that is simple and therefore can be learned and understood by beginners, intermediate and advanced developers. The creation of it was done by Guido Van Rossum in the year 1991. About supports different paradigms of programming like procedural, object-oriented and...
5 min read
An Introduction to Short-Circuiting Short circuiting makes the evaluation of logical expressions better. When you're dealing with if-statements or tricky true/false logic, you often use "and", "or", and "not" to mix different conditions. But in many cases, you can figure out the final result of a...
6 min read
In order to ensure code quality and constancy, unit testing is a fundamental component of software development. The unittest system in Python offers a steady environment for making and executing tests. Taking care of exceptions is a typical testing practice. To guarantee that the code...
4 min read
The tremendous rise of user-generated material, particularly on social media and internet platforms, has made monitoring unsuitable or explicit photos increasingly vital. One such problem is recognizing and screening nude or explicit content in order to provide consumers with a safe and acceptable environment. Python, with...
19 min read
The matplotlib.pyplot.annotate() function in the pyplot module of matplotlib enables users to add text to a figure at a specific point. Annotations are useful for highlighting specific points or adding additional information to plots. Let us understand what Annotate is. Annotate means to label something. For...
5 min read
Introduction: In this tutorial, we are learning to iterate over a set in Python. In Python, the Set is a collection of data types that are unordered, iterable, mutable, and have no equivalent elements. It is the unordered collection of unequal objects. This can be done...
11 min read
Introduction Data manipulation is a fundamental aspect of data analysis, and Python's Pandas library is a powerful tool for this purpose. One particularly useful feature of Pandas is the str.extract() method, which allows you to extract substrings from a Series of strings using regular expressions. In...
3 min read
? In the world of programming, geolocation data is crucial for a wide range of applications, from mapping services to weather forecasting. One common task is to retrieve the longitude and latitude of a city based on its name. Python, with its rich ecosystem of libraries,...
4 min read
We request you to subscribe our newsletter for upcoming updates.
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