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.

Categorized in:

Python,

Last Update: March 20, 2024