Class 11 Practical Programs
Count Vowels, Consonants, Uppercase, Lowercase Characters
AIM
To write a Python program that counts and displays the number of vowels, consonants, uppercase, and lowercase characters in a given string.
Character Classifications:
- Vowels: a, e, i, o, u (both uppercase and lowercase)
- Consonants: All alphabetic characters except vowels
- Uppercase: Characters from A to Z
- Lowercase: Characters from a to z
ALGORITHM
- Start
- Input a string from the user
- Initialize counters for vowels, consonants, uppercase, and lowercase to 0
- For each character in the string:
- Check if it's a vowel (a, e, i, o, u) - increment vowel counter
- Otherwise, if it's alphabetic - increment consonant counter
- For case counting, iterate through string again:
- Check if character is between 'a' and 'z' - increment lowercase counter
- Check if character is between 'A' and 'Z' - increment uppercase counter
- Display all counters
- Stop
PROGRAM
# Count and display the number of vowels, consonants, uppercase, lowercase characters in string.
str = input("Enter the String::\n")
vowels = 0
consonants = 0
for i in str:
if (i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or
i=='A' or i=='E' or i=='I' or i=='O' or i=='U'):
vowels = vowels + 1
else:
consonants = consonants + 1
print("The number of vowels:", vowels)
print("The number of Consonants:", consonants)
# UPPERCASE and LOWERCASE LOGIC
str1 = input("Enter the String::\n")
upper = 0
lower = 0
for i in range(len(str1)):
if(str1[i] >= 'a' and str1[i] <= 'z'):
lower += 1
elif(str1[i] >= 'A' and str1[i] <= 'Z'):
upper += 1
print('Lower case letters =', lower)
print('Upper case letters =', upper)OUTPUT
Test Case 1:
Enter the String::
Hello World
The number of vowels: 3
The number of Consonants: 8
Enter the String::
Hello World
Lower case letters = 8
Upper case letters = 2
Test Case 2:
Enter the String::
Programming123
The number of vowels: 3
The number of Consonants: 11
Enter the String::
Programming123
Lower case letters = 10
Upper case letters = 1
Test Case 3:
Enter the String::
PYTHON programming
The number of vowels: 4
The number of Consonants: 13
Enter the String::
PYTHON programming
Lower case letters = 11
Upper case letters = 6
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
Why do we count consonants as total characters minus vowels?
In this program, consonants include all non-vowel characters including spaces, digits, and special characters. A better approach would be to check if the character is alphabetic first.
What is the difference between str[i] and iterating directly over characters?
Direct iteration (for i in str) gives you the character directly, while index-based iteration (for i in range(len(str))) gives you the index, requiring str[i] to access the character.
How can we improve the vowel checking logic?
We can use: if i.lower() in 'aeiou' which is more concise and handles both cases automatically.
What happens if the string contains numbers or special characters?
Numbers and special characters are counted as consonants in the current logic. We should add a check using isalpha() method to count only alphabetic characters.
Can we use built-in string methods for this task?
Yes, we can use methods like isupper(), islower(), isalpha() to make the code more readable and efficient.
Why do we ask for input twice in this program?
The program has two separate sections - one for vowel/consonant counting and another for case counting. This could be optimized to use a single input and single loop.
How would you modify this to handle Unicode characters?
For Unicode support, we should use methods like isupper(), islower() instead of ASCII range comparisons, as they work with international characters.
What is the time complexity of this algorithm?
The time complexity is O(n) where n is the length of the string, as we iterate through the string twice (once for vowels/consonants, once for case counting).
Related Resources
- Strings in Python
- String Methods Tutorial
- String Operations PDF
- String Manipulation MCQs
- String Palindrome Program
String Tips
- • Use built-in methods like isupper(), islower()
- • Consider Unicode characters for international text
- • Optimize with single loop for multiple counts
- • Handle special characters appropriately