check if a string in python contains any vowels

Python – Check If String Contains Vowels

In this tutorial, we will look at how to check if a string in Python contains any vowel character or not with the help of some examples.

How to check if a string contains any vowel characters?

check if a string in python contains any vowels

You can use a combination of the Python built-in any() function and the membership operator in to check if a Python string contains any vowels or not.

The built-in any() function in Python takes an iterable as an argument and returns True if any value in the iterable is truthy (evaluates to True in a boolean context).

So, for each character in the string, check if it is a vowel character or not using the in keyword (by checking if the character is in aAeEiIoOuU) and pass the resulting boolean iterable as an argument to the any() function. The following is the syntax –

# check if string contains any vowel characters
any(ch in 'aAeEiIoOuU' for ch in s)

It returns True if any (that is, at least one) character in the given string is a vowel.

Examples

Let’s now look at some examples. First, we will create some strings that we will be using throughout this tutorial.

# create strings
s1 = "Adam"
s2 = "dry"
s3 = ""

# display the strings
print(s1)
print(s2)
print(s3)

Output:

Adam
dry

Here, we created three strings – s1, s2, and s3. The string s1 contains a vowel character, the string s2 does not contain any vowel characters and the string s3 is an empty string.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Let’s use a list comprehension to check if each character in a string is a vowel or not (by checking if it is a character in 'aAeEiIoOuU') and pass the resulting boolean list as an argument to the any() function.

# check if string contains any vowel characters
print(any([ch in 'aAeEiIoOuU' for ch in s1]))
print(any([ch in 'aAeEiIoOuU' for ch in s2]))
print(any([ch in 'aAeEiIoOuU' for ch in s3]))

Output:

True
False
False

We get True for s1 as it does contain at least one vowel letter. We get False for s2 as it does not contain any vowels. Note that we get False for an empty string.

The any() function takes an iterable as an argument, you can also directly pass an iterator which results in an iterable to the any() function (and avoid using the list comprehension).

# check if string contains any vowel characters
print(any(ch in 'aAeEiIoOuU' for ch in s1))
print(any(ch in 'aAeEiIoOuU' for ch in s2))
print(any(ch in 'aAeEiIoOuU' for ch in s3))

Output:

True
False
False

We get the same results as above.

Now, you can also convert the string to lowercase and check if a character is in 'aeiou' to check if it has one (or more) vowel characters in the string.

For example,

# check if string contains any vowel characters
print(any(ch in 'aeiou' for ch in s1.lower()))
print(any(ch in 'aeiou' for ch in s2.lower()))
print(any(ch in 'aeiou' for ch in s3.lower()))

Output:

True
False
False

We get the same result as above.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top