Python Program to Check Character is Lowercase or Not

In this article, we will learn about python program to check character is lowercase or not.

Getting Started

While writing program, we may have encountered a scenario where we need to check if entered character is in lowercase or not. or, if it is in uppercase or not.

We have already covered on how to check if entered character is in uppercase or not.

So, we will write a python program that checks if entered character is in lowercase or not. For example,

If user enters m, it returns True.
If user enters L, it returns False.
If user enters 3, it returns False.

We can do so in many ways –

Simple Comparison

We can simply compare if entered character lies between a and z or not as below –

def isLowerCharacter(ch):
    if len(ch) != 1 :
        return None
    else :
        return (ch >= 'a' and ch <= 'z')

ch = input('Enter a character')
print(isLowerCharacter(ch))

Output:

Enter a character
g
True

In isLowerCharacter method,

  • len() function checks if length of entered character is 1 or not. If it is not 1, it means we have entered an invalid value. So, isLowerCharacter function returns None.
  • If length of entered character is 1, then we check if it lies between a and z or not using (ch >= ‘a’ and ch <= 'z') condition.
  • If condition satisfies, then isLowerCharacter function returns True. Otherwise, it returns False.

Using ASCII Value

Using ASCII value of character, we can compare and find out if entered character is in lowercase or not. For example,

def isLowerCharacter(ch):
    if len(ch) != 1 :
        return None
    else :
        return (ord(ch) >= 97 and ord(ch) <= 122)

ch = input('Enter a character')
print(isLowerCharacter(ch))

Output:

Enter a character
p
True

In above program,

  • ord() function returns ASCII value of given character. Also, ASCII value of lowercase letter a is 97 and z is 122
  • At first, we take any character as input. Then, compares if it’s length is 1 or not. If yes, we get ascii value of that character.
  • Once we have ascii value, we compare if it lies between 97 and 122 or not. If yes, it is in lowercase. Otherwise, it’s not.

Using built-in method – islower()

islower() method is used with string to check if all characters are in lowercase or not. We can also use it with a single character to check if it’s in lowercase or not. For example,

def isLowerCharacter(ch):
    if len(ch) != 1 :
        return None
    else :
        return ch.islower()

ch = input('Enter a character')
print(isLowerCharacter(ch))

Output:

Enter a character
y
True

In above program,

  • islower() method returns True if character is in lowercase. Otherwise, it returns False.

That’s how we can write a python program to check character is lowercase or not. You can also checkout our other program examples in python.

Leave a Reply