Uppercase Alphabet Count for a given String in Python

In this tutorial, we will learn how to count number of uppercase letters in a string in Python. For a better understand, we have provided an example with the output. We hope you will easily understand how to count uppercase alphabets in a string in Python.

UPPERCASE ALPHABET COUNT in Python

Uppercase Alphabet Count and Lowercase Alphabets Count:

  1. First, the user will input a string (with/without space).
  2. Then, the program will count the number of uppercase and lowercase alphabets.
  3. The output will show the counts.

count uppercase letters in Python
Let’s take a look at the code snippet.

PROGRAM: Count number of uppercase letters in Python

s = input("Input a String: ")       #input string here
d={"UPPER CASE":0, "LOWER CASE":0}  #declare d as a dictionary
for c in s:
    if c.isupper():                 #checks whether the character is uppercase or not
        d["UPPER CASE"]+=1
    elif c.islower():               #checks whether the character is lowercase or not
        d["LOWER CASE"]+=1
    else:
        pass                        #passes to the loop if it's not an alphabet
print("Number of Uppercase Characters:",d['UPPER CASE'])
print("Number of Lowercase Characters:",d['LOWER CASE'])

OUTPUT 1:

Input a String: Priyam Sur
Number of Uppercase Characters: 2
Number of Lowercase Characters: 7

OUTPUT 2:

Input a String: AAbbCCddEEffGGhhIIjjKKll
Number of Uppercase Characters: 12
Number of Lowercase Characters: 12

So, we saw how to count uppercase and lowercase characters from an input string. Now, what if we have to convert lowercase alphabets to uppercase alphabets and uppercase alphabets to lowercase alphabets. Look out for more codes in this site under the Python section, you will find the solution to this problem too.



Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *