Python Program to Check if Character is Lowercase or Uppercase

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

Getting Started

Note: We have already covered on how to –

Visit above links if you need a program to check either lowercase or uppercase separately.

What if we need to write a python program that checks both condition in same program ?

This article handles both conditions in same program. So, if you have already gone through above two programs, you can skip this article and move on to next program.

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

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

We can do so in many ways –

Simple Comparison

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

def checkUppercaseOrLowercase(chr):
    if len(chr) != 1 :
        return "Invalid"
    else :
        str = "Invalid"
        if (chr >= 'a' and chr <= 'z'):
            str = "Lowercase"
        elif (chr >= 'A' and chr <= 'Z'):
            str = "Uppercase"
        return str    

ch = input('Enter any character')
print("Enter character is: " + checkUppercaseOrLowercase(ch))

Output:

Enter any character
P
Enter character is: Uppercase

In above code,

  • Condition (chr >= ‘a’ and chr <= 'z') checks if entered character is in lowercase or not.
  • Condition (chr >= ‘A’ and chr <= 'Z') checks if entered character is in uppercase or not.

Using ASCII Value

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

def checkUppercaseOrLowercase(chr):
    if len(chr) != 1 :
        return "Invalid"
    else :
        str = "Invalid"
        asc = ord(chr)
        if (asc >= 65 and asc <= 90):
            str = "Uppercase"
        elif (asc >= 97 and asc <= 122):
            str = "Lowercase"
        return str

ch = input('Enter any character')
print("Enter character is: " + checkUppercaseOrLowercase(ch))

Output:

Enter any character
t
Enter character is: Lowercase

In above program,

  • ord() function returns ASCII value of given character.
  • ASCII value of lowercase letters are from 97 to 122 and that of uppercase letters are from 65 to 90.
  • To check if entered character is in uppercase or lowercase, ascii value of entered letter is compared in each range.

Using built-in method – isupper() and islower()

islower() method is used with string to check if all characters are in lowercase or not.
isupper() method is used with string to check if all characters are in uppercase or not.

Using above methods, we can find out if entered character is in uppercase or lowercase.

def checkUppercaseOrLowercase(ch):
    if len(ch) != 1 :
        return "Invalid"
    else :
        str = "Invalid"
        if ch.islower():
            str = "Lowercase"
        elif ch.isupper():
            str = "Uppercase"
        return str

ch = input('Enter any character')
print("Enter character is: " + checkUppercaseOrLowercase(ch))

Output:

Enter any character
H
Enter character is: Uppercase

We use built-in methods in above python program to check if character is lowercase or uppercase.

Checkout other python programs at – https://tutorialwing.com/python-tutorial/

Reference – https://docs.python.org/3/tutorial/index.html

Leave a Reply