w3resource

Python Exercises: Match beginning and end of a word with a vowel

Python Regular Expression: Exercise-57 with Solution

From Wikipedia,
A vowel is a syllabic speech sound pronounced without any stricture in the vocal tract. Vowels are one of the two principal classes of speech sounds, the other being the consonant.

Write a Python program that checks whether a word stars and ends with a vowel in a given string. Return true if a word matches the condition; otherwise, return false.

Sample Data:
("Red Orange White") -> True
("Red White Black") -> False
("abcd dkise eosksu") -> True

Sample Solution:

Python Code:

import re
def test(text):
	return bool(re.findall('[/^[aeiou]$|^([aeiou]).*\1$/', text))

text ="Red Orange White"
print("Original string:", text)
print("Check beginning and end of a word in the said string with a vowel:")
print(test(text))
text ="Red White Black"
print("\nOriginal string:", text)
print("Check beginning and end of a word in the said string with a vowel:")
print(test(text))
text ="abcd dkise eosksu"
print("\nOriginal string:", text)
print("Check beginning and end of a word in the said string with a vowel:")
print(test(text))

Sample Output:

Original string: Red Orange White
Check beginning and end of a word in the said string with a vowel:
True

Original string: Red White Black
Check beginning and end of a word in the said string with a vowel:
False

Original string: abcd dkise eosksu
Check beginning and end of a word in the said string with a vowel:
True

Flowchart:

Flowchart: Regular Expression -  Match beginning and end of a word with a vowel.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: Convert a given string to snake case.
Next Python Exercise: Two following words begin and end with a vowel.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting sorted iterable (can sort by "compare" function):

>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]

>>> sorted(a,key=abs)
[1, 2, -3]