How to check if a string contains only lower case letters in Python?


A string is a group of letters that may be used to represent a single word or a whole statement. Strings are easy to use in Python since they don't need to be declared explicitly and may be defined with or without a specifier.

For manipulating and accessing strings, Python includes several built in functions and methods. In Python string is an object of the class String.

In this article, we will discuss how to check if a string contains only lower case letters in Python. There are multiple approaches to this.

Using the islower() method

One way to verify the lower case letters in a string is using the islower() method of string library. This method returns True if every character in the current string is lower and returns False otherwise.

Example 1

In the example given below, we are taking 2 strings str1 and str2, and checking if they contain any characters other than lower case alphabets. We are checking with the help of the islower() function.

str1 = 'abcdef' str2 = 'Abcdef' print("Checking whether",str1,"is lower case") print(str1.islower()) print("Checking whether",str2,"is lower case") print(str2.islower())

Output

The output of the above program is,

('Checking whether', 'abcdef', 'is lower case')
True
('Checking whether', 'Abcdef', 'is lower case')
False

Example 2

Following is another example for this using the islower() method −

In the program given below, we are checking what would happen if there are spaces in between the lowercase words.

str1 = 'welcome to tutorialspoint' print("Checking whether",str1,"is lower case") print(str1.islower())

Output

The output of above program is,

('Checking whether', 'welcome to tutorialspoint', 'is lower case')
True

Using the regular expressions

We can also use the regular expressions to determine whether the given string contains lower case letters. To do so, import the re library and install it if it isn't already installed.

After importing the re library, we'll use the regular expression "[a z]+$". This will return False if the string contains any characters other than lowercase characters; otherwise, True will be returned.

Example

In this program given below, we are using the regular expression ‘[a z]+$’ to check whether the given string is lowercased or not.

import re str1 = 'abcdef' str2 = 'Abcdef' print("Checking whether",str1,"is lower case") print(bool(re.match('[a z]+$', str1))) print("Checking whether",str2,"is lowercase") print(bool(re.match('[a z]+$', str2)))

Output

The output to the above program is,

('Checking whether', 'abcdef', 'is lower case')
False
('Checking whether', 'Abcdef', 'is lowercase')
False

Using ASCII values

We can iterate through each character of the string and verify based on the ASCII values. We know that ASCII values of lower case letters start at 97, so we have to check whether each ASCII value is greater than 97 or not. If each ASCII value is greater than 97, then True is returned, or else False is returned.

Example

In the below given example, we are writing a function checkLower() and comparing the ASCII values for every character in that string. If every character’s ASCII value is greater than 96 and less than 122 then True is returned otherwise False is returned.

def checkLower(str1): n = len(str1) count = 0 for i in str1: if(122>= ord(i) >= 97): count += 1 if count == n: return True return False str1 = 'abcdef' str2 = 'Abcdef' print("Checking whether",str1,"is lower case") print(checkLower(str1)) print("Checking whether",str2,"is lower case") print(checkLower(str2))

Output

The output of the above program is,

('Checking whether', 'abcdef', 'is lower case')
True
('Checking whether', 'Abcdef', 'is lower case')
None

Updated on: 19-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements