lower() method converts all uppercase letters in a string to lowercase. It returns a new string and does not modify the original string because strings are immutable. Characters that are already lowercase, as well as digits, spaces and symbols, remain unchanged.
Example 1: In this example, lower() converts all uppercase letters in the string to lowercase.
text = "HELLO World!"
print(text.lower())
Output
hello world!
Explanation: text.lower() converts all uppercase letters in text to lowercase. Spaces and punctuation marks remain unchanged.
Syntax
string.lower()
- Parameters: This method does not take any parameters.
- Return Value: Returns a new string with all uppercase letters converted to lowercase.
Examples
Example 1: In this example, lower() is used to convert all uppercase letters in a sentence to lowercase.
msg = "WELCOME TO PYTHON"
print(msg.lower())
Output
welcome to python
Example 2: In this example, the string contains letters, numbers and special characters. Only the alphabetic characters are affected by lower().
text = "CODE123@PYTHON!"
print(text.lower())
Output
code123@python!
Example 3: In this example, lower() is used to normalize text entered in different letter cases.
city = "NeW YoRK"
print(city.lower())
Output
new york
Case-Insensitive Comparison Using lower()
When comparing strings, differences in letter case can produce incorrect results. Converting both strings to lowercase ensures a case-insensitive comparison.
Example 1: In this example, two strings with different letter cases are compared after converting both to lowercase.
a = "Python"
b = "PYTHON"
print(a.lower() == b.lower())
Output
True
Explanation: a.lower() and b.lower() both become "python", so the comparison returns True.
Example 2: In this example, a keyword is searched within a sentence without worrying about letter case.
text = "Learn Data Science With PYTHON"
word = "python"
print(word.lower() in text.lower())
Output
True
Explanation: text.lower() and word.lower() convert both strings to lowercase before performing the search.