In this simple python program, we need to count the vowels in string using sets. It is a number-based python program.
For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:
In this simple python program on sets, we need to count the vowels in a string using sets. A set in python is defined as a collection of iterable data which is unordered. Sets can be represented by the braces which we use to represent the mathematical set. Set values must be unique, which means the set does not have any duplicate values in it.
Using a set, we can check for a given element that is present in the set using a data structure called has a table, which is much faster than the list. Python elements cannot be changed, which means immutable, but we can add or remove the elements from the set.
To implement the logic in this basic python program, we need to accept a string from the user using the input
method in python and save that string into a variable. Initialize a count variable to zero and initialize a set of vowels in the variable. Now We open a for loop
up to the end of the string. Then we open an if condition to check each letter in the string is a string or not. If it is a match, we increment the count variable by one. Finally, we print the count variable using the print
function in python.
STEP 1: Accept a string from the user and save that into a variable using the input
method in python programming.
STEP 2: Initialize a count to zero and add a set of vowels.
STEP 3: Use a for loop to traverse to the length of the string.
STEP 4: Use the if
condition in for loop
to check each letter in the string is a vowel or not.
STEP 5: If the condition satisfies, increment the count with one.
STEP 6: Use the print
statement in python language to print the count of vowels.
s=input("Enter the string to check the vowels count:")
count = 0
vowels = set("aeiou")
for letter in s:
if letter in vowels:
count += 1
print("Vowel count in the string:")
print(count)
Enter the string to check the vowels count: Python Program Vowel count in the string: 3