Python Forum
Counting vowels in a string (PyBite #106)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting vowels in a string (PyBite #106)
#1
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!
"""
Reply
#2
you need to iterate over chars in text and check if char is in vowels
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply
#4
you can look at alternative solutions here: https://python-forum.io/Thread-a-better-...iting-code
especially the micseydel's post#6
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove vowels in word with conditional ambrozote 12 4,005 May-02-2021, 06:57 PM
Last Post: perfringo
  counting vowels in a string project_science 3 2,510 Dec-30-2020, 06:44 PM
Last Post: buran
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,683 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 2,739 Sep-25-2020, 12:14 PM
Last Post: ndc85430
  Using my REPL to bisect numbers and lists with classes (PyBite #181) Drone4four 2 2,007 Sep-24-2020, 01:47 PM
Last Post: Drone4four
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 2,314 Jun-11-2020, 08:31 PM
Last Post: Drone4four
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,925 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  Counting the number of letters in a string alphabetically TreasureDragon 2 2,851 Mar-07-2019, 01:03 AM
Last Post: TreasureDragon
  Counting number of characters in a string Drone4four 1 3,411 Aug-16-2018, 02:33 PM
Last Post: ichabod801
  no vowels function alex_bgtv 6 4,981 Jan-01-2018, 08:48 PM
Last Post: alex_bgtv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020