DAY 8 of PYTHON top 100 questions : from Basic to Advanced !!

Hello, I'm Priya Chakraborty, a dedicated B.Tech student in Electrical Engineering at Siliguri Institute Of Technology. I'm an enthusiastic learner, constantly seeking to expand my skill set and knowledge base. With a solid foundation in programming languages such as C, R, Python, and MySQL, I'm poised to tackle complex technical challenges.
But my passions extend beyond the realm of engineering. I'm also an aspiring content writer, driven by a curiosity to communicate ideas, both technical and non-technical, in a way that captivates and educates.
My journey is defined by a relentless pursuit of self-improvement, coupled with strong communication skills. I believe in the power of continuous learning and the importance of sharing knowledge.
Write a Python program to count the number of vowels in a given string :
vowels = set("aeiouAEIOU")
count = 0
string = input("Enter a string: ")
for char in string:
if char in vowels:
count += 1
print("The number of vowels in the string is:", count)
Output :
Enter a string: priya
The number of vowels in the string is: 2
The program defines a set of vowels to check against. It uses the
set()function to create a set of the characters "a", "e", "i", "o", and "u", as well as their uppercase equivalents.By creating a set, the program ensures that each character appears only once in the set, and it can check if a character is a vowel by using the
inoperator, which checks if an item is in a set in constant time.The program initializes a counter to zero. This counter will be used to keep track of the number of vowels in the string. The program prompts the user to enter a string by calling the built-in
input()function.The user's input will be stored in the
stringvariable. The program iterates over each character in the string by using for loop.For each character in the string, it checks if the character is in the set of vowels defined earlier. If the character is a vowel, the program increments the counter by 1. Finally, the program prints the final count of vowels in the string by using the
print()function.The output will be a string that includes the message "The number of vowels in the string is:" followed by the value of the
countvariable.
If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).
HAPPY LEARNING !!!




