Python Different Counts in String

By | January 12, 2023

Python Different Counts in String – Python program to count uppercase letters, lower case letters, vowels, consonants and spaces in the given string.

Python Different Counts in String

 

#Python program to count upper case letters, lower case letters
#vowels, consonants and spaces in a string

uppercase=0
lowercase=0
vowels=0
consonants=0
spaces=0

string=input("Enter a String: ")

for ch in  string:
    if (ch.isupper()):
        uppercase+=1

    if (ch.islower()):
        lowercase+=1
    if (ch in ('A', 'a', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U')):
        vowels+=1
    elif (ch.isalpha()):
        consonants+=1
    if (ch.isspace()):
        spaces+=1

print("The given string contains...")
print("%d Uppercase letters"%uppercase)
print("%d Lowercase letters"%lowercase)
print("%d Vowels"%vowels)
print("%d Consonants"%consonants)
print("%d Spaces"%spaces)

Output

Enter a String: Hello World
The given string contains…
2 Uppercase letters
8 Lowercase letters
3 Vowels
7 Consonants
1 Spaces

 63 total views,  1 views today

Leave a Reply

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