• 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
  • VBA
  • About
You are here: Home / Python / How to Check if a String Contains Vowels in Python

How to Check if a String Contains Vowels in Python

February 8, 2022 Leave a Comment

In Python, we can easily check if a string contains vowels using a for loop and check individually if each character is a vowel or not.

def containsVowels(string):
    string = string.lower()
    for char in string:
        if char in "aeiou":
           return True
    return False

print(containsVowels("Hello World!"))

#Output:
True

When working with strings, it can be useful to know if there are any vowels contained in a string variable.

In Python, we can easily get if a string contains 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 check if a string has any vowels or not for you in a string using Python.

def containsVowels(string):
    string = string.lower()
    for char in string:
        if char in "aeiou":
           return True
    return False

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

#Output:
True
True
True
False

Checking if a Vowel Appears in a String Using Python

The example above is useful if you want to check if any vowel is in a string. We can also use Python to check if each of the 5 vowels appear in a string.

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

Below is a Python function which will check if a string has a particular vowel.

def checkEachVowel(string):
    checks = {}
    string = string.lower()
    for vowel in "aeiou":
        checks[vowel] = string.count(vowel) > 0
    return checks

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

#Output:
{'a': False, 'e': True, 'i': False, 'o': True, 'u': False}
{'a': True, 'e': True, 'i': True, 'o': True, 'u': False}
{'a': True, 'e': False, 'i': False, 'o': False, 'u': True}
{'a': False, 'e': False, 'i': False, 'o': False, 'u': False}

Hopefully this article has been useful for you to check if a string contains vowels using Python.

Other Articles You'll Also Like:

  • 1.  Format Numbers as Currency with Python
  • 2.  Using Python to Count Number of False in List
  • 3.  Check if Number is Larger than N in Python
  • 4.  Python Prepend to List with insert() Function
  • 5.  How Clear a Set and Remove All Items in Python
  • 6.  Python Add Months to Datetime Variable Using relativedelta() Function
  • 7.  nunique pandas – Get Number of Unique Values in DataFrame
  • 8.  Writing a Table Faster to Word Document Using python-docx
  • 9.  Python Add Days to Date Using datetime timedelta() Function
  • 10.  Using Python to Read File Word by Word

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

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy