How to Convert a String to Lowercase in Python
In Python, there are multiple ways to convert a string to lowercase. Following are a few of the most common methods:
Using the lower() method
The lower() method returns a string in all lowercase characters.
Using the casefold() method
The casefold() method returns a string in all lowercase characters and is similar to lower(). The difference between them is that casefold() is more aggressive in its conversion and can handle certain characters that lower() cannot.
Using the str.lower() method
This method is similar to using lower() method, but instead of calling the method on the string itself, you can call it as a function of str class.
Using the map() function
The map() function applies a function to each element of an iterable. you can use map() function to apply lower() method to each character of the string.
Using a for loop
You can loop over each character in the string and convert it to lowercase.
Using list comprehension
You can use list comprehension to create a new list of lowercase characters and join them together to form the new lowercase string.
All of the above methods will convert the string to lowercase in Python.
Lowercase first character of String in Python
To lowercase the first character of a string in Python, you can use the the lower() method.
In the above example,take the first character of the string using slicing (my_string[:1]), convert it to lowercase using the lower() method, and concatenate it with the rest of the string (my_string[1:]). Note that the second letter of the string remains capitalized in this example.