Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
counting vowels in a string
#4
in addition to excellent advises from @jefsummers and @deanhystad - you can also use list comprehension

txt = "Count the number of vowels in this sentence."
vowels = "aeiouAEIOU"
cnt = sum(ch in vowels for ch in txt)
print(f'Number of vowels in the sentence is {cnt}')
Output:
Number of vowels in the sentence is 13
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


Messages In This Thread
counting vowels in a string - by project_science - Dec-30-2020, 05:18 PM
RE: counting vowels in a string - by jefsummers - Dec-30-2020, 05:59 PM
RE: counting vowels in a string - by deanhystad - Dec-30-2020, 06:06 PM
RE: counting vowels in a string - by buran - Dec-30-2020, 06:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  remove vowels in word with conditional ambrozote 12 6,524 May-02-2021, 06:57 PM
Last Post: perfringo
  Counting vowels in a string (PyBite #106) Drone4four 4 3,335 Jul-07-2020, 05:29 AM
Last Post: theknowshares
  Counting the number of letters in a string alphabetically TreasureDragon 2 3,850 Mar-07-2019, 01:03 AM
Last Post: TreasureDragon
  Counting number of characters in a string Drone4four 1 4,378 Aug-16-2018, 02:33 PM
Last Post: ichabod801
  no vowels function alex_bgtv 6 6,221 Jan-01-2018, 08:48 PM
Last Post: alex_bgtv
  replace vowels niru 9 23,437 Sep-26-2017, 12:46 PM
Last Post: nilamo
  Vowels and Consonants detector OmarSinno 5 11,210 Sep-21-2017, 02:27 PM
Last Post: buran

Forum Jump:

User Panel Messages

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