Count Uppercase and Lowercase in Python

Count uppercase and lowercase in python | Uppercase letters are also known as capital letters. Uppercase letters signal to the reader that something is important or significant. English alphabet uppercase letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

In writing, most letters are lowercase. Lowercase letters are all letters that do not begin a sentence or refer to a proper noun. English alphabet lowercase letters: a b c d e f g h i j k l m n o p q r s t u v w x y z.

Python Program to Count Number of Uppercase and Lowercase letters in a String

This python program using the built-in function to count the number of uppercase and lowercase characters in a string. We used For Loop to count uppercase and lowercase. The islower() function is used to check if the string contains any lowercase characters. Similarly, the isupper() function is used to check if the string contains any uppercase characters.

# Python program to count uppercase and lowercase characters

# take input
string = input('Enter any string: ')

upper, lower = 0, 0
for i in string:
    #count lowercase characters
    if(i.islower()):
        lower = lower + 1
    #count uppercase characters
    elif(i.isupper()):
        upper = upper + 1

# print number of lowercase characters
print('Lowercase characters:',lower)
# print number of uppercase characters
print('Uppercase characters:',upper)

Output for the different input values:-

Enter any string: Know Program
Lowercase characters: 9
Uppercase characters: 2

Enter any string: PYThon
Lowercase characters: 3
Uppercase characters: 3

Enter any string: Count UPPERCASE and Lowercase Characters
Lowercase characters: 24
Uppercase characters: 12

We can also write this program in a simple way to count the number of uppercase and lowercase characters.

string = input('Enter any string: ')

lower = [x for x in string if x.islower()]
upper = [x for x in string if x.isupper()]

print('Lowercase characters:', len(lower))
print('Uppercase characters:', len(upper))

Python Program to Count Upper and Lower case Characters without using inbuilt Functions

# Python program to count uppercase and lowercase characters

# take input
string = input('Enter any string: ')

upper, lower = 0, 0
for i in string:
    #count lowercase characters
    if(i>='a' and i<='z'):
        lower = lower + 1
    #count uppercase characters
    elif(i>='A' and i<='Z'):
        upper = upper + 1

# print number of lowercase characters
print('Lowercase characters:',lower)
# print number of uppercase characters
print('Uppercase characters:',upper)

Output:-

Enter any string: PYTHON program
Lowercase characters: 7
Uppercase characters: 6

Count Upper and Lowercase in Python using ASCII value

The ord() method is used to find Unicode value of a character passed as its argument.

# Python program to count uppercase and lowercase characters

# take input
string = input('Enter any string: ')

upper, lower = 0, 0
for i in range(len(string)):
    #count lowercase characters
    if(ord(string[i]) >= 97 and ord(string[i]) <= 122):
        lower = lower + 1
    #count uppercase characters
    elif(ord(string[i]) >= 65 and ord(string[i]) <= 90):
        upper = upper + 1

# print number of lowercase characters
print('Lowercase characters:',lower)
# print number of uppercase characters
print('Uppercase characters:',upper)

Output:-

Enter any string: ASCII value
Lowercase characters: 5
Uppercase characters: 5

Program using Collections Function

This Python program calculates the number of uppercase letters and lowercase letters in one line code using collections.Counter method. Collections in Python are containers that are used to store collections of data.

import collections
string = input('Enter any string: ')
count = collections.Counter("upper" if x.isupper() 
            else "lower" if x.islower() else "" for x in string)
print(count)

Output:-

Enter any string: Collections.Counter
Counter({‘lower’: 16, ‘upper’: 2, ”: 1})

Also See:- Python program to convert uppercase to lowercase

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply