Python Program to Count Upper and Lower Case Characters [3 Ways]

If you want to learn how to count the number of upper and lower-case characters in Python, you are in the right place.

In this Python tutorial, I will explain three methods to write a Python program to count upper and lower case characters, such as using a for loop and a while loop. Moreover, I will show how to count the number of upper and lower case characters by importing the collections module and the Counter class.

Let’s begin to learn the methods with examples:

Python program to count number of uppercase and lowercase letters in a string using the for loop

This is the source code of Python to count the number of uppercase and lowercase letters in a string using the for loop.

The for loop is used to iterate over the Python string. So, we can go through the string and check for conditions using the if-else conditional statements in Python.

USA_capital =input("Enter capital of USA: ")
count_of_lowercase=0
count_of_uppercase=0
for i in USA_capital :
    if(i.islower()):
        count_of_lowercase = count_of_lowercase+1
    elif(i.isupper()):
        count_of_uppercase = count_of_uppercase+1
print("The number of lowercase characters is:")
print(count_of_lowercase)
print("The number of uppercase characters is:")
print(count_of_uppercase)

I have used the i.isupper() and i.islower() methods to generate expressions to count the occurrences of uppercase and lowercase letters in Python, respectively, in the elements of the Counter.

if(i.islower()):
        count_of_lowercase = count_of_lowercase+1
    elif(i.isupper()):
        count_of_uppercase = count_of_uppercase+1

Output:

Enter capital of USA: Washington D.C.
The number of lowercase characters is:
9
The number of uppercase characters is:
3

You can see the result in the screenshot below to count the number of uppercase and lowercase letters in a string using the for loop in Python

Python program for uppercase and lowercase

Calculate the number of upper / lower case letters in a string in Python using a while loop

This is another approach to count the number of upper/lower case letters in a string in Python using a while loop.

A while loop in Python is also used to iterate through each input string character, and similarly, we can apply the condition as the above example.

You can see the complete Python program to calculate the number of upper and lower case letters in a string using a while loop in Python.

def count_upper_lower(input_string):
    uppercase_count = 0
    lowercase_count = 0
    index = 0
    while index < len(input_string):
        if input_string[index].isupper():
            uppercase_count += 1
        elif input_string[index].islower():
            lowercase_count += 1
        index += 1
    print("Number of uppercase letters:", uppercase_count)
    print("Number of lowercase letters:", lowercase_count)
user_input = input("Enter the USA state: ")
count_upper_lower(user_input)

Here, within the while loop in Python, Check the characters of the current index. If the character is an uppercase letter, the uppercase_count is incremented, similarly to the lowercase letters.

 while index < len(input_string):
        if input_string[index].isupper():
            uppercase_count += 1
        elif input_string[index].islower():
            lowercase_count += 1
        index += 1

Output:

Enter the USA state: Florida
Number of uppercase letters: 1
Number of lowercase letters: 6

The below screenshot provides the result of the Python code after the code runs successfully.

Python Program to Count Upper and Lower Case Characters

Python Program to Count Upper and Lower Case Characters by importing the collections module and the Counter class.

This is another method to count uppercase and lowercase letters in a string in Python by using the collections module and the Counter class.

The Counter class will keep the count of the occurrences of each item in the Python string.

The complete code of Python to count upper and lower case characters in string using the collections module and the Counter class.

 from collections import Counter
def count_upper_lower(cities):
    char_counts = Counter(cities)
    upper_count = sum(char.isupper() for char in char_counts.elements())
    lower_count = sum(char.islower() for char in char_counts.elements())
    print("Uppercase letters:", upper_count)
    print("Lowercase letters:", lower_count)

input_string = input("Enter a USA city name: ")
count_upper_lower(input_string)

I added the ‘from collections import Counter’ statement to import the Counter class from the collections module. Counter class counts occurrences of each character in the input string.

from collections import Counter

Here, the sum() function is used to sum up the counts of uppercase and lowercase letters in Python.

upper_count = sum(char.isupper() for char in char_counts.elements())
    lower_count = sum(char.islower() for char in char_counts.elements())

Output:

Enter a USA city name: San Francisco
Uppercase letters: 2
Lowercase letters: 10

Refer to the screenshot below to see the result of the Python program to count upper and lower case characters.

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

Conclusion

In this Python tutorial, we looked at how the isupper() and islower() method in Python is used to count the uppercase and lowercase letters in a given string with a for loop. Also, we explained how to calculate the number of upper / lower case letters in a string in Python using a while loop.

Additionally, we looked into a Python program to count upper and lower case characters. I have used the “from collections import Counter” statement to import the Counter class from the collections module to count upper and lowercase letters in Python.

I hope this article helps you write a Python Program to Count Upper and Lower Case Characters and the uses of different methods in Python.

You may also like to read: