How to count the number of vowels and consonants in a string in Python
![how to count number of vowels and consonants in a string in python](https://pythonpoint.net/wp-content/uploads/2021/01/how-to-count-number-of-vowels-and-consonants-in-a-string-in-python-1024x576.jpg)
In this article, we will learn how to count the number of vowels and consonants in a string Python. In english alphabet, the letters ‘a, e, i, o, u’ constitute the vowels and rest of the alphabets as consonants.
vcnt = 0 ccnt = 0 vowels = ['a', 'e', 'i', 'o', 'u'] string = 'hello world, good morning' for i in string: if i in vowels: vcnt += 1 else: ccnt += 1 print("No of vowels: ", vcnt) print("No of consonants: ", ccnt)
Output:
No of vowels: 7 No of consonants: 18
In this program, initially the count of vowels and consonants are set to 0. Later we defined a list consisting of vowels as elements. Using the for loop and if-else condition, we are checking if a particular letter belong in vowel’s list. If yes, then count of vowels is incremented one or else count of consonants is incremented by one.
Subscribe
Login
Please login to comment
0 Discussion