• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Count Vowels in a String Using Python

How to Count Vowels in a String Using Python

February 8, 2022 2 Comments

In Python, we can easily count how many vowels are in a string using a loop and counting the number of vowels we find in the string.

def countVowels(string):
    count = 0
    string = string.lower()
    for char in string:
        if char in "aeiou":
           count = count + 1
    return count

print(countVowels("Hello World!"))

#Output:
3

When working with strings, it can be useful to know how many vowels appear inside a variable.

In Python, we can easily get the count of vowels in a string looping over each character of the string and seeing if it is a vowel or not.

The vowels include “a”,”e”,”i”,”o”, and “u”.

Below is a function which will count the number of vowels for you in a string using Python.

def countVowels(string):
    count = 0
    string = string.lower()
    for char in string:
        if char in "aeiou":
           count = count + 1
    return count

print(countVowels("Hello World!"))
print(countVowels("This is a string with some words."))
print(countVowels("What's up?"))

#Output:
3
8
2

Counting the Number of Times Each Vowel Appears in a String Using Python

The example above is useful if you want to get the total number of vowels in a string. We can also use Python to get the count of how many times each vowel appears in a string.

To do this, we will loop over the vowels and create a dictionary which stores the count for each of the vowels.

Below is a function which will get the count of how many times each vowel appears in a given string.

def countEachVowel(string):
    counts = {}
    string = string.lower()
    for vowel in "aeiou":
        counts[vowel] = string.count(vowel)
    return counts

print(countEachVowel("Hello World!"))
print(countEachVowel("This is a string with some words."))
print(countEachVowel("What's up?"))

#Output:
{'a': 0, 'e': 1, 'i': 0, 'o': 2, 'u': 0}
{'a': 1, 'e': 1, 'i': 4, 'o': 2, 'u': 0}
{'a': 1, 'e': 0, 'i': 0, 'o': 0, 'u': 1}

Hopefully this article has been useful for you to count the number of vowels in a string using Python.

Other Articles You'll Also Like:

  • 1.  pandas idxmax – Find Index of Maximum Value of Series or DataFrame
  • 2.  Check if Line is Empty in Python
  • 3.  Print Object Attributes in Python using dir() Function
  • 4.  String Contains Case Insensitive in Python
  • 5.  Python Add Months to Datetime Variable Using relativedelta() Function
  • 6.  Convert pandas Series to List in Python
  • 7.  Using Python to Create Empty DataFrame with pandas
  • 8.  Using Python to Get Queue Size
  • 9.  Using Python to Split String by Newline
  • 10.  Union of Lists in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Comments

  1. M says

    May 2, 2022 at 9:00 am

    how to write else condition if no vowels found in string entered by user and print as no vowels found as output

    Reply
    • Erik says

      May 2, 2022 at 9:05 am

      You could probably do something like this:

      string_input = input("Enter a string to check for vowels")
      
      if countVowels(string_input) > 0: 
          print("Vowels found")
      else: 
          print("No vowels found")
      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy