Python Lowercase – How to Use the String lower() Method?
The lower() function in Python converts a string to its lowercase in python. A lowercase version of the string contains only lowercase characters, i.e., a lowercase string doesn't contain any capital characters.
For example, "Hello" is not a lowercase string as the capital "H" is present in "Hello".
And "world" is a lowercase string as all the characters of "world" are small letters.
Example of Python string lower() method
Suppose we have a string as "HELLO PYTHON", and we would like to convert this string to lowercase in python. We can convert this string to lowercase in Python using the lower() function as,
Output:
Syntax of lower() in Python
Parameters of Python string lower() method
The lower() function in Python doesn't take any parameters.
Return Values of lower() in Python
Python's lower() function returns a string containing lowercase characters only. The original string is returned if there are no uppercase characters in the provided string.
What is lower() in Python?
Python's lower() function converts a string to its lowercase version. Now, what is the lowercase version of a string? The lowercase version is the version that only contains small characters in the string. Hence a lowercase version of a string will not contain any capital characters.
For example, "hello" is a lowercase string, as all the characters of hello are small.
There are chances when a user may enter a string like "HeLLo pYthON," and this type of string is unformatted. So to format this string, we use the lower() function in Python.
More Examples
Convert a string to lowercase
Given a string s like "THiS iS a StRiNg", we have to convert this string to lowercase as "this is a string".
Output:
lower() in Python to Compare String
The lower() function in Python can be used to check whether two strings are same or not.
For example, the strings, "pyThon" and "PytHoN" are same as the lowercase version of both the string is same, "python".
Output:
Casefold() Function to Convert String to Lowercase
The casefold() in Python is similar to lower() in Python but with the key difference that casefold() can operate on Unicode characters while lower() in Python operates on ASCII characters.
Output:
Other Ways to Convert a Python String to Lowercase
Using map() with Lambda Function in lower() Method:
- This method applies the lower() method to each input string character using the map() function and a lambda function, converting the entire string to lowercase.
- The resulting lowercase characters are then joined together into the single string using the join() method.
Code:
Output:
Using List Join with lower() Method:
- Here, a list comprehension is used to apply the lower() method to each string element in the input list.
- The lowercase strings are then concatenated into a single string using the join() method, resulting in a lowercase version of the original string.
Code:
Output:
Using map and str.lower with lower() Method:
- This approach uses the map() function with the str.lower method to convert each string in the input list to lowercase.
- The resulting lowercase strings are stored in a list, providing a lowercase version of the original list of strings.
Code:
Output:
Using Swapcase() Function:
- The swapcase() function converts uppercase characters to lowercase and vice versa for the entire input string.
- This method is useful for toggling the case of characters in a string without explicitly converting to lowercase.
Code:
Output:
Using casefold() Function:
- The casefold() function is similar to lower(), but it performs a more aggressive lowercase conversion, which is suitable for case-insensitive comparisons.
- It converts all characters in the input string to lowercase, including any special or Unicode characters with uppercase equivalents.
Code:
Output:
Conclusion
- The lower() in Python converts a string to its lowercase version. Syntax of lower() in Python is string.lower().
- The lower() function in Python doesn't take any parameters.
- The original string is returned if there are no uppercase characters in the provided string.