Python Forum

Full Version: Counting vowels in a string (PyBite #106)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I’m trying to count the number of vowels contained inside Tim Peter’s famous “Zen of Python” prose. I didn’t get it on my first attempt (with a for loop). I didn’t get it on my second attempt (with the count method) either.

There are 262 vowels. So that is my expected output.

Tim Peter's text in full can be found at the bottom of this post.

Here is my first attempt:

 vowels = ('a', 'e', 'i', 'o', 'u')
# vowels = 'aeiou'
result = 0
for vowels in text:
   result = result + 1
print(result)
I tried commenting in and out the first two lines. Regardless of which one is in play, the output I receive is ‘858’ (which is the total number of characters in Tim’s text). So that is not where I want to be.

Here is my second attempt:

vowels = 'aeiou'
vowel_count = text.count(vowels)
print(vowel_count)
The output this time is: 0, which is also not where I want to be. When I replace the first line with this instead: vowels = ('a', 'e', 'i', 'o', 'u') the output then changes to a TypeError which says: TypeError: must be str, not tuple. So I was more correct to keep vowels = 'aeiou', even though that still does not print out the desired result (262).

Here is my question for all of you: What might you people suggest I try next?

In the effort to answer my own question, so far I’ve referenced these as a guide:
I previously wrestled with a similar issue in a forum post titled, “Counting number of characters in a string”. In that thread, I was playing with a larger text file and trying to count only the number of characters in the last line of the file. The syntax I am using today is some what similar but I am struggling to figure out why my output today is 0 or 858 when I am now trying to count the number of vowels.

I’ve also leveraged Python’s built in help() function.

My effort to learn how to count vowels in a text string is one component to a larger PyBites (#106) exercise that I am working on. This is not course material for school.

text = """
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
you need to iterate over chars in text and check if char is in vowels
(Jul-03-2020, 10:46 AM)buran Wrote: [ -> ]you need to iterate over chars in text and check if char is in vowels

Thank you, my friend!

Here is my script now:

vowels = 'aeiou'
# vowels = ('a', 'e', 'i', 'o', 'u')
result = 0
for char in text:
    if char.lower() in vowels:
        result = result +1
print(result)
The output is exactly: 262. I did it!

I just wish I could have come up with this on my own instead of having to write a long winded, overly analytical question when the solution was so clearly obvious.

Thanks again, @buran. Smile
you can look at alternative solutions here: https://python-forum.io/Thread-a-better-...iting-code
especially the micseydel's post#6
vowels = ['a', 'e', 'i', 'o', 'u']
count = 0
text = "hello,how are you?"
for ch in text:
if ch.lower() in vowels:
count = count +1
print(count)

Output: 7