Home ยป Count Vowels and Consonants in a String Correctly using python

Count Vowels and Consonants in a String Correctly using python

Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
1 Year Subscription
Spring Framework Basics Video Course
Java SE 11 Programmer II [1Z0-816] Practice Tests
Oracle Java Certification

In this article we show how to count a vowel or consonant in python

We will also show you a commonly found piece of code that does this incorrectly, I've added this because I think its important to realize about finding bugs, even subtle ones that don't cause catastrophic failures in your program and also verify code that you find on websites – yes, even this one.

Working Example 1

We use a function to check if a character is a vowel or consonant. If a match is found then a variable is incremented.

We also check for upper case or lower case vowels and consonants

This approach eliminates incorrect counts, we will show a source code example that is commonly displayed on some python sites which is completely wrong

def categorise_string(str_in: str):
    vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y',
                  'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y',
                  'Z'}
    num_vowels = 0
    num_consonants = 0

    for char in str_in:
        if char in vowels:
            num_vowels += 1
        elif char in consonants:
            num_consonants += 1

    return num_vowels, num_consonants


mysentence = input('Please input your sentence with a full stop: ')
number_vowels, number_consonants = categorise_string(mysentence)

print('vowels: {}, consonants: {}'.format(number_vowels, number_consonants))

Here are a couple of runs, changing the case of some of the text

>>> %Run vowelsconsonants1.py
Please input your sentence with a full stop: TeSt ExAmpLe
vowels: 4, consonants: 7
>>> %Run vowelsconsonants1.py
Please input your sentence with a full stop: test example
vowels: 4, consonants: 7
>>> %Run vowelsconsonants1.py
Please input your sentence with a full stop: TEST EXAMPLE
vowels: 4, consonants: 7

Failing Example 1

Now lets look at this example

str1 = input("Please Enter Your String : ")
vowels = 0
consonants = 0

for i in str1:
    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("Total Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", consonants)

remember in the previous example the correct number is vowels: 4, consonants: 7. Lets try the same text

>>> %Run failingexample1.py
Please Enter Your String : test example
Total Number of Vowels in this String =  4
Total Number of Consonants in this String =  8

That's wrong, what has happened is that the code above has counted the white space as well as consonants. This is because they only specify the vowels to be counted and everything is counted as a consonant !!!!!!

You can try another character.

Lets try this sentence

test example!

>>> %Run failingexample1.py
Please Enter Your String : test example!
Total Number of Vowels in this String =  4
Total Number of Consonants in this String =  9

That's worse, its counted the white space and the exclamation mark. This is a very poor bug and yes its on a couple of other python sites. Not good

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More