0
0
PythonProgramBeginner · 2 min read

Python Program to Count Vowels and Consonants in String

You can count vowels and consonants in a string by looping through each character and checking if it is a vowel using if char.lower() in 'aeiou' and counting consonants otherwise; for example, use vowels += 1 and consonants += 1 inside the loop.
📋

Examples

Inputhello
OutputVowels: 2, Consonants: 3
InputPython Programming
OutputVowels: 4, Consonants: 13
Input123!@#
OutputVowels: 0, Consonants: 0
🧠

How to Think About It

To count vowels and consonants, first think about what characters are vowels (a, e, i, o, u). Then, go through each letter in the string one by one. If the letter is a vowel, add one to the vowel count. If it is a letter but not a vowel, add one to the consonant count. Ignore any characters that are not letters.
📐

Algorithm

1
Get the input string from the user.
2
Initialize two counters: one for vowels and one for consonants.
3
Loop through each character in the string.
4
Check if the character is a letter.
5
If it is a vowel, increase the vowel counter.
6
If it is a consonant, increase the consonant counter.
7
After the loop ends, return or print the counts.
💻

Code

python
string = input("Enter a string: ")
vowels = 0
consonants = 0
for char in string:
    if char.isalpha():
        if char.lower() in 'aeiou':
            vowels += 1
        else:
            consonants += 1
print(f"Vowels: {vowels}, Consonants: {consonants}")
Output
Enter a string: hello Vowels: 2, Consonants: 3
🔍

Dry Run

Let's trace the input 'hello' through the code

1

Initialize counters

vowels = 0, consonants = 0

2

Check 'h'

'h' is a letter and not a vowel, consonants = 1

3

Check 'e'

'e' is a vowel, vowels = 1

4

Check 'l'

'l' is a consonant, consonants = 2

5

Check second 'l'

'l' is a consonant, consonants = 3

6

Check 'o'

'o' is a vowel, vowels = 2

7

Print result

Vowels: 2, Consonants: 3

CharacterIs Letter?Is Vowel?Vowels CountConsonants Count
hYesNo01
eYesYes11
lYesNo12
lYesNo13
oYesYes23
💡

Why This Works

Step 1: Check if character is a letter

We use char.isalpha() to make sure we only count letters, ignoring numbers or symbols.

Step 2: Identify vowels

We convert the character to lowercase and check if it is in the string 'aeiou' to count vowels.

Step 3: Count consonants

If the character is a letter but not a vowel, we count it as a consonant.

🔄

Alternative Approaches

Using collections.Counter
python
from collections import Counter
string = input("Enter a string: ").lower()
vowels = 'aeiou'
counter = Counter(c for c in string if c.isalpha())
vowel_count = sum(counter[c] for c in vowels)
consonant_count = sum(counter[c] for c in counter if c not in vowels)
print(f"Vowels: {vowel_count}, Consonants: {consonant_count}")
This method uses a built-in counter to count all letters first, then sums vowels and consonants separately; it is concise but uses extra memory.
Using regular expressions
python
import re
string = input("Enter a string: ").lower()
vowels = len(re.findall(r'[aeiou]', string))
consonants = len(re.findall(r'[bcdfghjklmnpqrstvwxyz]', string))
print(f"Vowels: {vowels}, Consonants: {consonants}")
This method uses regex to find vowels and consonants directly; it is clean but requires understanding regex.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through each character once, so the time grows linearly with the string length.

Space Complexity

Only a few counters are used, so space stays constant regardless of input size.

Which Approach is Fastest?

The simple loop method is fastest and uses least memory; alternatives like Counter or regex add overhead but can be more concise.

ApproachTimeSpaceBest For
Simple loopO(n)O(1)Beginners, fast and memory efficient
collections.CounterO(n)O(n)Counting all letters with built-in tools
Regular expressionsO(n)O(n)Clean code if familiar with regex
💡
Always convert characters to lowercase before checking vowels to handle uppercase letters easily.
⚠️
Counting non-letter characters like digits or symbols as consonants or vowels.