I am brand new to python -
I have to build a function called 'first__vowel'
. ACCEPT a list of strings as input and RETURN the first string that starts with a lowercase vowel ("a","e","i","o", or "u")
. if no string starts with vowel, RETURN the empty string ("")
.
Can you help build this function.
Thanks
Check this:
vowels = ["a", "e", "i", "o", "u"]
def first_vowel(ss):
for s in ss:
if s and s[0] in vowels:
return s
return ""
Test:
first_vowel(["Drere", "fdff", "", "aBD", "eDFF"])
'aBD'
You need:
def first_vowel(lst):
# iterate over list using for loop
for i in lst:
# check if first letter is vowel
if i and i[0] in ['a','e','i','o','u']:
return i
return ""
k = ['sad','dad','mad','asd','eas']
print(first_vowel(k))
Or You can also use regex
import re
def first_vow(lst):
pat = re.compile(r'^[aeiou][a-zA-Z]*')
for i in lst:
match = re.match(pat, lst)
if match:
return i
return ""
k = ['sad','Aad','mad','','asd','eas']
first_vow(k)
You can always use str.startswith()
function too:
def first_vowel(lst):
for elem in lst:
if elem.startswith(('a','e','i','o','u')):
return elem
return ''
k = ['', 'b','sad','dad','mad','asd','eas']
print(first_vowel(k))
l1 = ['hi', 'there', 'its', 'an' , 'answer', '']
def first_vowel(var):
for i in var:
if i.startswith(('a', 'e', 'i', 'o', 'u')): return i
first_vowel(l1) # output-> 'its'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.