如何在python中的所有元音上使用“start with”?

2024-06-28 11:17:14 发布

您现在位置: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函数,它应该打印以元音开头的单词


Tags: 字符串inforifdefwithanyword
1条回答
网友
1楼 · 发布于 2024-06-28 11:17:14

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 vowel
isdl starts with a vowel
usdlw starts with a vowel

相关问题 更多 >