Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[-]
Welcome
You have to register before you can post on our site.

Username:


Password:





[-]
Discord Widget

[-]
Twitter: SickGaming

[-]
Latest Threads
[Tut] Your Python Regex Pattern Doesn’t ...
Last Post: xSicKxBot
Today 12:54 AM
» Replies: 0
» Views: 4
[Oracle Blog] The Arrival of Java 16
Last Post: xSicKxBot
Today 12:54 AM
» Replies: 0
» Views: 13
[Tut] Create WooCommerce WordPress Conta...
Last Post: xSicKxBot
Today 12:54 AM
» Replies: 0
» Views: 4
(Indie Deal) FREE Hope Lake, 2K & Team17...
Last Post: xSicKxBot
Today 12:54 AM
» Replies: 0
» Views: 4
(Free Game Key) Bomber Crew - Free Steam...
Last Post: xSicKxBot
Today 12:54 AM
» Replies: 0
» Views: 6
News - Diablo Immortal: Release Time, Pr...
Last Post: xSicKxBot
Today 12:54 AM
» Replies: 0
» Views: 4
PC - We Were Here Forever
Last Post: xSicKxBot
Today 12:54 AM
» Replies: 0
» Views: 5
[Oracle Blog] JDK 16 is live!
Last Post: xSicKxBot
05-25-2022 09:58 PM
» Replies: 0
» Views: 21
[Tut] 5 Pythonic Ways to Print a List wi...
Last Post: xSicKxBot
05-25-2022 09:57 PM
» Replies: 0
» Views: 6
(Indie Deal) Spy Fox Giveaways, Hello Ga...
Last Post: xSicKxBot
05-25-2022 09:57 PM
» Replies: 0
» Views: 7

[Tut] How to Count Vowels in a String

#1
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.

import re
from collections import Counter

Method 1: Use Regex and Dictionary Comprehension


This example uses Regex and Dictionary Comprehension as a one-liner to tally the number of specified vowels in a string. The results return in a Dictionary format.

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 saying. Then, Dictionary Comprehension converts the string to lowercase and re.findall() searches for and tallies each specified vowel.

The results save to vcount and are output to the terminal.




Output


{'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.

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 saying. Then, List Comprehension converts the string to lowercase and searches for and tallies each specified vowel.

The results save to variable vcount and are output to the terminal.




Output


[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 Counter() to count the number of specified vowels in a string.

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 saying and initiates the Counter() object, count.

A for loop instantiates and traverses through each character converting to lowercase, searching for and tallies each specified vowel.

The results save to count and are output to the terminal.

If this code was output to the terminal using print(count), the output would be as follows:

Output using print(count)


Counter({'a': 8, 'o': 4, 'e': 3, 'u': 3})

Placing count inside dict() removes the word Counter and surrounding braces ().

Output using print(dict(count))


{'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 for loop and string.count() to tally the number of specified vowels in a string. The results return as a string.

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 saying and initiates a variable tmp.

A for loop instantiates and traverses through each character, searching for and tallying each specified vowel. The results convert to a string, save to tmp, and are output to the terminal.

Output


a:8 e:3 i:0 o:4 u:3


Method 5: Use map() and count()


This example uses map() and count() to tally the number of specified vowels in a string.

saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
print(*map(saying.lower().count, 'aeiou'))

This code declares the string, saying converts the string to lowercase, and tallies the specified vowel. The results are output to the terminal.




Output


8 3 0 4 3


Summary


In this case, lower() was not required as you could see no vowels were in uppercase. However, you may not always know what a string will contain. In this case, best to convert to either lowercase or uppercase.

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!




https://www.sickgaming.net/blog/2022/05/...-a-string/
Reply





Users browsing this thread:
1 Guest(s)

𝚂̷𝚒̷𝚌̷𝚔̷𝙶̷𝚊̷𝚖̷𝚒̷𝚗̷𝚐̷ 𝙷̷𝚘̷𝚖̷𝚎̷

Copyright © SickGaming.net 2022