Python String lower() Method
The str.lower()
method converts all the characters of a string from uppercase to lowercase and returns the copy of a string. If the string does not have uppercase characters, it returns the original string.
Example:
text = "PYTHON IS AWESOME"
print(text.lower())
Output
python is awesome
Syntax
The Syntax of the str.lower()
function is:
str.lower()
Here, str
is a string.
Parameters
The str.lower()
function does not take any parameters.
Return Value
The str.lower()
function returns the copy of a lowercase string. It converts all the uppercase characters into lowercase, and if the string does not contain uppercase characters, it returns the original string as-is.
Example 1: Python Program to convert string from uppercase to lowercase
# Text with Mixed Case
text = "PYTHON is both Compiled as well as an Interpreted Language"
print(text.lower())
# Alphanumeric with special characters Text
text = "There are 7 DAYS in a WEEK !!!"
print(text.lower())
Output
python is both compiled as well as an interpreted language
there are 7 days in a week !!!
Example 2: Convert all the elements in the list to lowercase
The Program iterates all the elements in the list and prints them in lowercase.
programming_languages = ["PYTHON","C#","GO","JAVA","JAVASCRIPT"]
for ele in programming_languages:
print(ele.lower())
Output
python
c#
go
java
javascript
Example 3: How lower() method is used in the Program
Let's say if you have an application that stores the username in lowercase and if the user enters their username in uppercase or title case, you first convert them into lowercase using the lower()
method and check if it exists in the Database.
username_list = ["admin", "user1", "jack", "w3basic"]
user = input("Enter your username:")
if user.lower() in username_list:
print("Username Found")
else:
print("Invalid Username")
Output
Enter your username:ADMIN
Username Found
Enter your username:Administrator
Invalid Username
Reference: Python Official Docs