Counting Vowels and Consonants in Python

Day 4/365: Counting Vowels and Consonants in a String 🔤🧠 Today I worked on a classic beginner-friendly problem in Python: counting how many vowels and consonants are present in a string. The logic I used was simple but powerful: 1. Start with a string (for example: "Hello"). 2. Loop through each character. 3. If the character is in the set of vowels (aeiouAEIOU), increase the vowel counter. 4. Otherwise, if it’s an alphabet and not a vowel, increase the consonant counter. 5. In the end, print the total number of vowels and consonants. What I liked about this exercise: It made me think carefully about conditions (if / elif) and how loops work. I learned why it’s important to handle only alphabetic characters, so spaces, commas, or digits don’t get counted as consonants by mistake. It’s a small step, but it builds the habit of thinking about edge cases and input validation. Day 4 done ✅ 361 more to go. If you have any interesting variations of this problem (like handling entire sentences or ignoring case and punctuation), share them with me—I’d love to try them out. #100DaysOfCode #365DaysOfCode #Python #LogicBuilding #StringManipulation #CodingJourney #LearnInPublic #AspiringDeveloper

  • graphical user interface, text

Since you are interested in variations to the solution, I'm going to suggest normalizing the data. This will help you avoid errors down the line. Basically, convert everything to lower case. Now inside of your loop, you can have a more straightforward validation check: if the character is in "aeiou", since we are no longer interested in uppercase letters.

To view or add a comment, sign in

Explore content categories