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?
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- Python – Check If String Contains Lowercase Letters
- Python – Check If String Contains Uppercase Letters
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python – Capitalize First Letter of Each Word
- Python – Check If All Characters in String are Same
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.