Python Different Counts in String – Python program to count uppercase letters, lower case letters, vowels, consonants and spaces in the given 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)
The Python program provides many conts of different types in a string. For example it will display the number of
- Uppercase letters
- Lowercase letters
- Vowels
- Consonants
- and Spaces
Output: Here is some sample output of this program run.
Enter a String: Hello World
The given string contains…
2 Uppercase letters
8 Lowercase letters
3 Vowels
7 Consonants
1 Spaces
Explanation of key parts of the program:
- A series of variables (
uppercase
,lowercase
,vowels
,consonants
, andspaces
) are initialized to store the counts of different character types. - The
input()
function is used to take user input for the string that needs to be analyzed. - A
for
loop iterates through each character (ch
) in the input string. - Inside the loop:
- The
isupper()
method is used to check if the current character is in uppercase, and if so, theuppercase
counter is incremented. - The
islower()
method is used to check if the current character is in lowercase, and if so, thelowercase
counter is incremented. - A tuple containing the lowercase and uppercase vowels is used to check if the character is a vowel. If it is, the
vowels
counter is incremented. - The
isalpha()
method is used to check if the character is an alphabetic character (i.e., not a digit or special character), and if so, theconsonants
counter is incremented. - The
isspace()
method is used to check if the character is a space, and if so, thespaces
counter is incremented.
- The
- After the loop completes, the program prints the counts of each type of character based on the gathered data.
The program’s purpose is to provide insights into the composition of characters within the input string, helping the user understand the distribution of uppercase letters, lowercase letters, vowels, consonants, and spaces.
- Python Program to convert string in Sentence case
- Python Program to convert string to Opposite case
- Python Program to Convert String to Title Case
- Python Program to convert a string to lower case
- Python Program to Convert String to Upper Case
- Python Program To Show Calendar of a Year
- Python Program to output Calendar for a month
- Palindrome Program in Python
- Python String Program To Count Vowels
- Python Text File Program To Count Vowels
- Python File Program Count Characters in Text File
- Python Exception Handling FileNotFoundError
- Python Text File Read and Show File Contents
- Python file program read numbers write squares