Python Program to Count Number of Vowels in a String using Sets

This is a Python Program to count the number of vowels present in a string using sets

Problem Description

The program takes a string and creates a dictionary with key as first character and value as words starting with that character.

Problem Solution

1. Enter a string and store it in a variable.
2. Initialize a count variable to 0.
3. Create a set containing vowels.
4. Use a for loop to traverse through the letters in the string.
5. Using an if statement, check if the letter in the string is equal to a vowel.
6. If it is equal, increment the vowel count.
7. Print the final count of the vowels.
8. Exit.

Program/Source Code

Here is source code of the Python Program to create a dictionary with key as first character and value as words starting with that character. The program output is also shown below.

s=raw_input("Enter string:")
count = 0
vowels = set("aeiou")
for letter in s:
    if letter in vowels:
        count += 1
print("Count of the vowels is:")
print(count)
Program Explanation

1. User must enter a string and store it in a variable.
2. The count variable is initialized to 0.
3. A set containing vowels is created using set().
4. A for loop is used to traverse through the letters in the string.
5. An if statement checks if the letter in the string is equal to a vowel.
6. If it is equal, the vowel count is incremented.
7. The final count of the vowels is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter string:Hello world
Count of the vowels is:
3
 
Case 2:
Enter string:Python Program
Count of the vowels is:
3

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.