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

To check if a string in Python ends with a vowel or not, check if the last character in the string is a vowel using the membership operator in
.
The membership operator in
in Python, returns True
if a value is in a collection (for example, a character is present in a string) and returns False
otherwise.
So, use the membership operator in
to check if the last character in the string (character at index -1) is one of the vowel characters or not. The following is the syntax –
# check if string ends with a vowel s[-1].lower() in "aeiou"
Here, we use the string lower()
function to make the vowel check case-insensitive. It returns True
if the string ends with a vowel.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Examples
Let’s now look at some examples. First, we will create some strings that we will use throughout this tutorial.
# create strings s1 = "apple" s2 = "leaf" s3 = "$500" s4 = "" # display the strings print(s1) print(s2) print(s3) print(s4)
Output:
apple leaf $500
Here, we created four strings – s1
, s2
, s3
and s4
. The string s1
ends with a vowel, the string s2
does not end with a vowel, the string s3
ends with a digit character '0'
and the string s4
is an empty string.
Let’s now check if the strings s1
, s2
, and s3
end with a vowel or not.
# check if string ends wtih a vowel print(s1[-1].lower() in "aeiou") print(s2[-1].lower() in "aeiou") print(s3[-1].lower() in "aeiou")
Output:
True False False
We get True
for s1
as it does end with a vowel. We get False
for s2
and s3
as they do not end with a vowel.
Now let’s apply the above method to the empty string, s4
.
# check if string ends wtih a vowel print(s4[-1].lower() in "aeiou")
Output:
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [3], in <module> 1 # check if string ends wtih a vowel ----> 2 print(s4[-1].lower() in "aeiou") IndexError: string index out of range
We get an IndexError
because we’re trying to access an index that does not exist (an empty string does not have a -1
index).
To avoid the above error, you can first check whether the string is non-empty and then proceed to check if the last character is a vowel or not. See the example below –
# function to check if string ends with a vowel def check_str_ends_with_vowel(s): if s and s[-1].lower() in "aeiou": return True else: return False # check if string ends with a vowel print(check_str_ends_with_vowel(s4))
Output:
False
We get False
as the output for the empty string.
You might also be interested in –
- Python – Check If String Starts with any in a List
- Python – Check If String Starts with a Vowel
- Python – Check If String starts with a Number
- Python – Check If String Starts with a Letter
- Python – Check If a Character Appears Twice in String
- Python – Check If String Contains Vowels
- 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.