The Python lower() function converts a string to all lowercase. The Python isLower() method will check if the alphabetical characters in a string are all lowercase and return True or False. The lower() and isLower() functions are useful for fields like email where all letters should be lowercase.
When you’re working with a string in Python, you may want to convert the contents of that string to lowercase.
For example, you may be creating a sign up form that converts a user’s email to lowercase. This is a common practice used to ensure that another account cannot be created using the same email in uppercase because strings are case-sensitive.
The Python built-in function lower() can be used to convert a string into lowercase and return a copy of the revised string. In addition, the Python isLower() method can be used to check whether all the characters within a string appear in lowercase.
In this tutorial, we will discuss how to use the lower() and isLower() methods to work with lowercase strings. We will explore examples for each of these methods to show how they can work in a Python program.
lower() Python Method
The Python lower() method converts all characters in a string to lowercase. Numbers and special characters are left unchanged. lower() is added to the end of a Python string value.
lower() is one of Python’s string methods. It works on all cased characters in a string. Here’s the syntax for the Python lower() function:
The lower() function takes in no parameters.
To show off how lower() works, we’ll use an example.
Lowercase Python Example
Let’s say that we are creating a sign up form, and we want to convert the email address a user inserts into lowercase. We could do so by using the following code:
email = input("What is your email address?") print(email) final_email = email.lower() print(final_email)
Upon execution, our code returns the following strings:
What is your email address? aLex@gmail.com aLex@gmail.com firstname.lastname@example.org
We declare a variable called email that collects an email address from the user. In this case, we inserted “aLex@gmail.com” as our email. Then, we print out that email to the console in the format that it appeared when it was entered into the Python console.
On the next line, we declare a Python variable called final_email which converts the contents of the email variable to lowercase. Finally, we print final_email to the console, which returns our original string but in lowercase.
lower() will return symbols and numbers in their regular state because those characters are not case-sensitive. Only the Unicode characters in a string are converted to lowercase.
The Python isLower() method evaluates whether all characters in a string are lowercase. This method does not check numbers, spaces, and other non-alphabetical characters.
Before you convert a string to lowercase, you may want to evaluate whether that string is already in lowercase. That’s where the isLower() method comes in.
isLower() returns a True or False value based on whether the string contains only lowercase characters. Here’s the syntax for the Python isLower() method:
Like the lower() method, isLower() does not take any parameters. Instead, it is appended to the end of a string value.
isLower() Python Example
Let’s use an example to showcase how the lower() method works. For instance, before we convert a user’s email to lowercase, we want to check if it is already in lowercase. We could do so using this code:
email = input("What is your email address?") print(email.isLower())
If we run our program and insert the email “aLex@gmail.com”, our code returns the following:
What is your email address? aLex@gmail.com False
The string “aLex@gmail.com” contains one uppercase character—the L—and so the isLower() method decided that it was False. Meanwhile, if we were to insert the email “email@example.com” into our program, we would receive the following output:
What is your email address? firstname.lastname@example.org True
isLower() will return True even if the string contains white space, digits, and/or symbols. Only lowercase letters that are found in the string will cause isLower() to evaluate it as False.
Say we want to run a particular block of code depending on whether a string contains an uppercase character. We could do this using a Python “if” statement.
Consider the following code:
if email.isLower(): print("This email is valid.") else: print("Email addresses can only contain lowercase characters.")
We use a Python if statement and the isLower() method to evaluate whether the user’s email only uses lowercase characters. If isLower() returns True, the “if” statement executes. Otherwise, our “else” statement executes.
If we inserted the email “email@example.com”, our code would return:
This email is valid.
The Python lower() method can be used to convert a string into lowercase and return a revised copy of the string. The Python isLower() function can be used to check whether a string contains an uppercase character.
In this tutorial, we’ve explored the two main Python lowercase methods: lower() and isLower(). We also examined a few examples of these methods in action. So now you have the knowledge you need to work with lowercase strings like a Python expert!
If you’re interested in reading more about coding in Python, check out our complete How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.