Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Count Vowels in a String

#1
How to Count Vowels in a String

<div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;344950&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;0&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn how to <strong>count the number of vowels in a string</strong>. </p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">To make it more fun, we have the following running scenario:</p>
<p><em>In Canada, we have a province called <a rel="noreferrer noopener" href="https://www.saskatchewan.ca/" data-type="URL" data-id="https://www.saskatchewan.ca/" target="_blank">Saskatchewan</a>. This province has a large amount of flat land. In this article, we reference their local saying. </em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write Python code to count the vowels in a String?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-search/" target="_blank">Regex</a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a></li>
<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-count/" data-type="URL" data-id="https://blog.finxter.com/python-list-count/" target="_blank"><code>count()</code></a></li>
<li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://docs.python.org/3/library/collections.html" data-type="URL" data-id="https://docs.python.org/3/library/collections.html" target="_blank"><code>Counter()</code></a> and <a rel="noreferrer noopener" href="https://docs.python.org/3/library/collections.html" data-type="URL" data-id="https://docs.python.org/3/library/collections.html" target="_blank"><code>count.update()</code></a></li>
<li><strong>Method 4</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-count/" data-type="URL" data-id="https://blog.finxter.com/python-list-count/" target="_blank"><code>count()</code></a></li>
<li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" data-type="URL" data-id="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-count/" data-type="URL" data-id="https://blog.finxter.com/python-list-count/" target="_blank"><code>count()</code></a></li>
</ul>
<hr class="wp-block-separator"/>
<p id="block-3b5c9c73-276c-4b84-a1bc-d5ba1de38d56">Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.</p>
<pre class="EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re
from collections import Counter</pre>
<hr class="wp-block-separator"/>
<h2>Method 1: Use Regex and Dictionary Comprehension</h2>
<p class="has-global-color-8-background-color has-background">This example uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-search/" target="_blank"><code>Regex</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank"><code>Dictionary Comprehension</code></a> as a one-liner to tally the number of specified vowels in a string. The results return in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionary</a> format.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p class="has-text-align-center has-base-3-color has-accent-background-color has-text-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />A Finxter Favorite!</p>
<p>This code declares the string <code>saying</code>. Then, <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank"></a><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary-comprehension/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary-comprehension/" target="_blank">Dictionary Comprehension</a> converts the string to <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank">lowercase</a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-re-findall/" data-type="URL" data-id="https://blog.finxter.com/python-re-findall/" target="_blank"><code>re.findall()</code></a> searches for and tallies each specified vowel. </p>
<p>The results save to <code>vcount</code> and are output to the terminal.</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python Regex re.search() - A Simple Guide with Example" width="780" height="439" src="https://www.youtube.com/embed/Mv2VVpUgypc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p><strong>Output</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>{'a': 8, 'e': 3, 'i': 0, 'o': 4, 'u': 3}</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator"/>
<h2>Method 2: Use List Comprehension and count()</h2>
<p class="has-global-color-8-background-color has-background">This example uses <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> to tally the number of specified vowels in a string. The results return in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> format.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
vcount = [saying.lower().count(x) for x in 'aeiou']
print(vcount)</pre>
<p>This code declares the string <code>saying</code>. Then, <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> converts the string to <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank">lowercase</a> and searches for and tallies each specified vowel. </p>
<p>The results save to variable <code>vcount</code> and are output to the terminal.</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python String Count Method - A Short Tutorial" width="780" height="439" src="https://www.youtube.com/embed/V3g4uE6j_E0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p>Output</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>[8, 3, 0, 4, 3]</code></td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note:</strong> This output displays the totals but not their associated vowel.</p>
<hr class="wp-block-separator"/>
<h2>Method 3: Use Counter() and count.update()</h2>
<p class="has-global-color-8-background-color has-background">This example calls the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/collections.html" data-type="URL" data-id="https://docs.python.org/3/library/collections.html" target="_blank">Collections</a> library and uses <a rel="noreferrer noopener" href="https://docs.python.org/3/library/collections.html" data-type="URL" data-id="https://docs.python.org/3/library/collections.html" target="_blank"><code>Counter()</code></a> to count the number of specified vowels in a string. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2,5-6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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))</pre>
<p>This code declares the string <code>saying </code>and initiates the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/collections.html" data-type="URL" data-id="https://docs.python.org/3/library/collections.html" target="_blank"><code>Counter()</code></a> object, <code>count</code>. </p>
<p>A <code><a href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank" rel="noreferrer noopener">for</a></code> loop instantiates and traverses through each character converting to <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank">lowercase</a>, searching for and tallies each specified vowel. </p>
<p>The results save to <code>count</code> and are output to the terminal.</p>
<p>If this code was output to the terminal using <code>print(count)</code>, the output would be as follows:</p>
<p><strong>Output </strong>using <code>print(count)</code></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>Counter({'a': 8, 'o': 4, 'e': 3, 'u': 3})</code></td>
</tr>
</tbody>
</table>
</figure>
<p>Placing <code>count</code> inside <code><a href="https://blog.finxter.com/python-dict/" data-type="post" data-id="19866" target="_blank" rel="noreferrer noopener">dict()</a></code> removes the word <code>Counter</code> and surrounding braces <code>()</code>.</p>
<p><strong>Output</strong> using <code>print(dict(count))</code></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>{'a': 8, 'e': 3, 'i': 0, 'o': 4, 'u': 3}</code></td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note:</strong> This method produces the same output as Method 1 but with four (4) additional lines of code.</p>
<hr class="wp-block-separator"/>
<h2>Method 4: Use For and count()</h2>
<p class="has-global-color-8-background-color has-background">This example uses a <a href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/"><code>for</code></a> loop and <a href="https://blog.finxter.com/python-string-count/" data-type="URL" data-id="https://blog.finxter.com/python-string-count/" target="_blank" rel="noreferrer noopener"><code>string.count()</code></a> to tally the number of specified vowels in a string. The results return as a string.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p>This code declares the string <code>saying </code>and initiates a variable <code>tmp</code>. </p>
<p>A <code><a href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank" rel="noreferrer noopener">for</a></code> loop instantiates and traverses through each character, searching for and tallying each specified vowel. The results convert to a string, save to <code>tmp</code>, and are output to the terminal.</p>
<p><strong>Output</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> a:8 e:3 i:0 o:4 u:3</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator"/>
<h2>Method 5: Use map() and count()</h2>
<p class="has-global-color-8-background-color has-background">This example uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" data-type="URL" data-id="https://blog.finxter.com/python-map/" target="_blank"><code>map()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-count/" data-type="URL" data-id="https://blog.finxter.com/python-list-count/" target="_blank"><code>count()</code></a> to tally the number of specified vowels in a string. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">saying = 'Saskatchewan! Where you can watch your dog run away for 3 days.'
print(*map(saying.lower().count, 'aeiou'))</pre>
<p>This code declares the string, <code>saying</code> converts the string to <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank">lowercase</a>, and tallies the specified vowel. The results are output to the terminal.</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Mastering the Python Map Function [+Video]" width="780" height="439" src="https://www.youtube.com/embed/tqph6mWC3m8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p><strong>Output</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code> 8 3 0 4 3</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator"/>
<h2>Summary</h2>
<p>In this case, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-lower/" data-type="URL" data-id="https://blog.finxter.com/python-string-lower/" target="_blank"><code>lower()</code></a> 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.</p>
<p>These five (5) methods of counting vowels in a string should give you enough information to select the best one for your coding requirements.</p>
<p>Good Luck &amp; Happy Coding!</p>
<hr class="wp-block-separator"/>
</div>


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



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016