![]() |
|
[Tut] How to Count Vowels in a String - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Count Vowels in a String (/thread-99392.html) |
[Tut] How to Count Vowels in a String - xSicKxBot - 05-14-2022 How to Count Vowels in a String Rate this post Problem Formulation and Solution Overview In this article, you’ll learn how to count the number of vowels in a string. To make it more fun, we have the following running scenario: In Canada, we have a province called Saskatchewan. This province has a large amount of flat land. In this article, we reference their local saying. Question: How would we write Python code to count the vowels in a String?We can accomplish this task by one of the following options:
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free. Code: import re
from collections import CounterMethod 1: Use Regex and Dictionary Comprehension This example uses Code: RegexCode: Dictionary ComprehensionCode: saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
vcount = {x: len(re.findall(f"{x}", saying.lower())) for x in 'aeiou'}
print(vcount) A Finxter Favorite!This code declares the string Code: sayingCode: re.findall()The results save to Code: vcountOutput Code: {'a': 8, 'e': 3, 'i': 0, 'o': 4, 'u': 3}Method 2: Use List Comprehension and count() This example uses List Comprehension to tally the number of specified vowels in a string. The results return in a List format. Code: saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
vcount = [saying.lower().count(x) for x in 'aeiou']
print(vcount)This code declares the string Code: sayingThe results save to variable Code: vcountOutput Code: [8, 3, 0, 4, 3] Note: This output displays the totals but not their associated vowel.Method 3: Use Counter() and count.update() This example calls the Collections library and uses Code: Counter()Code: saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
count = Counter() for i in saying: if i in 'aeiou': count.update(i.lower()) print(dict(count))This code declares the string Code: sayingCode: Counter()Code: countA Code: forThe results save to Code: countIf this code was output to the terminal using Code: print(count)Output using Code: print(count)Code: Counter({'a': 8, 'o': 4, 'e': 3, 'u': 3})Placing Code: countCode: dict()Code: CounterCode: ()Output using Code: print(dict(count))Code: {'a': 8, 'e': 3, 'i': 0, 'o': 4, 'u': 3} Note: This method produces the same output as Method 1 but with four (4) additional lines of code.Method 4: Use For and count() This example uses a Code: forCode: string.count()Code: saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
tmp = ''
for i in 'aeiou': tmp += i + ':' + str(saying.count(i)) + ' '
print(tmp)This code declares the string Code: sayingCode: tmpA Code: forCode: tmpOutput Code: a:8 e:3 i:0 o:4 u:3Method 5: Use map() and count() This example uses Code: map()Code: count()Code: saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
print(*map(saying.lower().count, 'aeiou'))This code declares the string, Code: sayingOutput Code: 8 3 0 4 3Summary In this case, Code: lower()These five (5) methods of counting vowels in a string should give you enough information to select the best one for your coding requirements. Good Luck & Happy Coding! |