Other Methods to Convert String to Lower Case
Let’s look at some other methods to convert a string to lowercase. There are multiple ways to complete a task in Python and we will discuss some lower() method alternatives below:
Convert String to lowercase using map With Lambda Function
In this example, the code converts the string “w3wiki” to lowercase using a lambda function and the map function. The result, “w3wiki,” is then printed. The same can be achieved more succinctly with `lowercased_string = original_string.lower()`.
Python3
original_string = "w3wiki" lowercased_string = ''.join( map ( lambda x: x.lower(), original_string)) print (lowercased_string) |
Output:
w3wiki
Convert String to lowercase using List Join
In this example, the code converts the string “Pratham Sahani” to lowercase using a list comprehension. The resulting lowercase string is then joined and printed.
Python3
original_string = "Pratham Sahani" lowercased_string = ''.join([c.lower() for c in original_string]) print (lowercased_string) |
Output :
pratham sahani
Convert String to lowercase using map and str.lower with lower() Method
In this example, the code converts the original string “w3wiki” to lowercase characters using the str.lower method. However, the map function needs to be wrapped in a list() or join() to apply the transformation to each character.
Python3
original_string = "w3wiki" lowercased_string = ''.join( map ( str .lower, original_string)) print (lowercased_string) |
Output :
w3wiki
Convert String to lowercase using Swapcase() Function
Convert uppercase to lowercase in Python using the swapcase() function. In this example, code defines a string ‘w3wiki’ in variable ‘s’. The `swapcase()` method is then applied to the string, converting uppercase letters to lowercase and vice versa.
Python3
s = 'w3wiki' print (s.swapcase()) |
Output:
w3wiki
Convert String to lowercase using casefold() Function
Convert uppercase to lowercase in Python using the casefold function. In this example code converts the string ‘w3wiki’ to its casefolded form, making it lowercase and suitable for case-insensitive comparisons.
Python3
s = 'w3wiki' print (s.casefold()) |
Output:
w3wiki
Python String lower() Method
Python string lower() method converts all letters of a string to lowercase. If no uppercase characters exist, it returns the original string.
Example:
Python3
string = "ConvErT ALL tO LoWErCASe" print (string.lower()) |
Output
convert all to lowercase