π Day 64/150 – Count Vowels in a String in Python
Vowels are the letters: a, e, i, o, u.
In Python, we can count vowels in a string using loops, list comprehensions, and functions.
Let’s explore different ways to count vowels in Python π
πΉ Method 1 – Using for Loop
text = "Python Programming" count = 0 for ch in text.lower(): if ch in "aeiou": count += 1 print("Vowel Count:", count)
✅ Output
Vowel Count: 4
π Loops through each character and increases the count when a vowel is found.
πΉ Method 2 – Taking User Input
text = input("Enter a string: ") count = 0 for ch in text.lower(): if ch in "aeiou": count += 1 print("Vowel Count:", count)
π Allows the user to enter any string dynamically.πΉ Method 3 – Using List Comprehension
text = "Python Programming" count = sum([1 for ch in text.lower() if ch in "aeiou"]) print("Vowel Count:", count)Vowel Count: 4
✅ Outputπ Short and Pythonic way to count vowels.
πΉ Method 4 – Using Function
def count_vowels(text): count = 0 for ch in text.lower(): if ch in "aeiou": count += 1 return count print(count_vowels("Python Programming"))4
✅ Outputπ Best approach when you want reusable code.
π₯ Key Takeaways
✅ lower() helps handle uppercase and lowercase letters
✅ in "aeiou" is an easy way to check vowels
✅ List comprehensions make code shorter
✅ Functions improve reusability and readability


0 Comments:
Post a Comment