Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Convert case of elements in a list of strings

Python | Convert case of elements in a list of strings

Given a list of strings, write a Python program to convert all strings from lowercase/uppercase to uppercase/lowercase.

Input : ['GeEk', 'FOR', 'gEEKS']
Output: ['geek', 'for', 'Lazyroar']

Input : ['fun', 'Foo', 'BaR']
Output: ['FUN', 'FOO', 'BAR']

  Method #1: Convert Uppercase to Lowercase using map function 

Python3




# Python code to convert all string
# from uppercase to lowercase.
 
# Using map function
out = map(lambda x:x.lower(), ['GeEk', 'FOR', 'gEEKS'])
 
# Converting it into list
output = list(out)
 
# printing output
print(output)


Output:

['geek', 'for', 'Lazyroar']

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the output list.

Method #2: Convert Lowercase to Uppercase using List comprehension 

Python3




# Python code to convert all string
# from uppercase to lowercase.
 
# Initialisation
input = ['fun', 'Foo', 'BaR']
 
# Converting
lst = [x.upper() for x in input]
 
# printing output
print(lst)


Output:

['FUN', 'FOO', 'BAR']

Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), where n is the number of elements in the input list, as a new list is created to store the converted strings.

Method #3: Using enumerate function 

Python3




lst = ['Foo', 'fOO', 'geeKs']
lst = [i.lower() for a,i in enumerate(lst)]
print(lst)


Output

['foo', 'foo', 'Lazyroar']

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as a new list is created to store the lowercase version of each string in the original list.

Method #4: Using casefold()

The approach is to use the str.casefold() method. This method is similar to the str.lower() method, but it is more aggressive in converting the string to a lowercase representation. It is particularly useful for handling case conversions involving non-ASCII characters, such as those found in the German language.

Here is an example of using str.casefold() to convert a list of strings to lowercase:

Python3




input = ['GeEk', 'FOR', 'gEEKS']
output = [s.casefold() for s in input]
print(output)
# Output: ['geek', 'for', 'Lazyroar']
#This code is contributed by Edula Vinay Kumar Reddy


Output

['geek', 'for', 'Lazyroar']

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the output list.

Method 5: Using the lower() 

Python3




input = ['fun', 'Foo', 'BaR']
 
lst = [x.lower() for x in input]
 
print(lst)


Output

['fun', 'foo', 'bar']

Time Complexity: O(n)

Auxiliary Space: O(n), where n is length of list.

Method #6: Using a for loop

Convert uppercase letters to lowercase by using a for loop to iterate through each character in the string, and then using the lower() function to convert the uppercase letter to lowercase.

Python3




# Python code to convert all string
# from uppercase to lowercase.
 
# Using a for loop
string = 'GeEk FOR gEEKS'
output = ''
for char in string:
    if char.isupper():
        output += char.lower()
    else:
        output += char
 
# printing output
print(output)


Output

geek for Lazyroar

Time complexity: O(n), where n is the length of the input string. This is because the for loop iterates through each character in the string exactly once.

Auxiliary space: O(n), as we are creating a new string (output) to store the lowercase version of the input string. 

RELATED ARTICLES

Most Popular

Recent Comments