猿问

如何在 python 中的所有元音上使用'开头'?

我只是偶然发现如何检查字符串是否以元音开头?


def f(s):

    s = s.split(' ')

    for word in s:

        if word.startswith(any('aeiou')):

            print('starts with a vowel')

    print(s)



r = 'd sljf l23j lekj 023 fls erj 50 isdl usdlw '


f(r)

但是它给出了错误,出了什么问题?any() 是一个 bool 函数,它应该打印出以元音字母开头的单词


慕容3067478
浏览 127回答 1
1回答

杨__羊羊

startswith接受字符串,你可以试试这个。r = 'd sljf l23j lekj 023 fls erj 50 isdl usdlw 'for x in r.split():    if any(x.startswith(v) for v in 'aeiou'):        print(f'{x} starts with a vowel')erj starts with a vowelisdl starts with a vowelusdlw starts with a vowel
随时随地看视频慕课网APP

相关分类

Python
我要回答