10

我正在尝试使用python编写一个函数来检查给定单词的第一个字母,例如“ball”是大写还是小写的元音。例如:

#here is a variable containing a word:
my_word = "Acrobat"

#letters in vowel as a list
the_vowel = ["a","e","i","o","u"]

如何检查“Acrobat”中的第一个字母是列表中的元音之一?我还需要考虑它是大写还是小写?

4

16 回答 16

21

尝试my_word[0].lower() in the_vowel

于 2012-11-14T12:57:52.267 回答
17

我不知道它是否比这里已经发布的答案更好,但你也可以这样做:

vowels = ('a','e','i','o','u','A','E','I','O','U')
myWord.startswith(vowels)
于 2012-11-14T13:04:38.223 回答
10

这里有一些提示可以帮助您弄清楚。

从字符串中获取单个字母的下标字符串。

>>> 'abcd'[2]
'c'

请注意,第一个字符是字符零,第二个字符是字符一,依此类推。

接下来要注意的是,大写字母不等于小写字母:

>>> 'a' == 'A'
False

upper幸运的是,python 字符串具有lower更改字符串大小写的方法:

>>> 'abc'.upper()
'ABC'
>>> 'a' == 'A'.lower()
True

要测试我们列表中的成员资格in

>>> 3 in [1, 2, 3]
True
>>> 8 in [1, 2, 3]
False

因此,为了解决您的问题,将下标绑定在一起以获得单个字母, upper/lower调整大小写,并使用in.

于 2012-11-14T13:00:01.700 回答
4
my_word = "Acrobat"
the_vowel = "aeiou"

if myword[0].lower() in the_vowel:
    print('1st letter is a vowel')
else:
    print('Not vowel')
于 2012-11-14T12:58:47.597 回答
2

我的代码看起来像这样。

original = raw_input("Enter a word:")
word = original.lower()
first = word[0]
vowel = "aeiou"

if len(original) > 0 and original.isalpha():
    if first in vowel:
        print word
        print first
        print "vowel!"
    else:
        print word
        print first
        print "consonant
于 2013-10-17T09:44:35.427 回答
2
x = (input ("Enter any word: "))
vowel = "aeiouAEIOU"
if x[0] in vowel:
    print ("1st letter is vowel: ",x)       
else:
    print ("1st letter is consonant: ",x)     
于 2014-12-11T21:41:18.187 回答
1

这是我的做法,因为在将输入的单词存储为变量之前需要先检查它:

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    if first in ['a','e','i','o','u']:
        print "vowel"
    else:
        print "consonant"
else:
    print 'empty'
于 2013-07-13T16:56:47.493 回答
1

这是 codecadmy.com 上练习的解决方案:

original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
vowel = "aeiou"

if len(original) > 0 and original.isalpha():
   if first in vowel:      
       print 'vowel'
   else:   
       print 'consonant'
else:
   print 'empty'
于 2013-05-24T11:37:12.700 回答
1

变化:

if my_word[0] in ('a','e','i','o','u'):
   print(' Yes, the first letter is vowel ')
else:
   print(' No, the first letter is not vowel ')

所以,这是找出第一个字母是元音还是不是元音的简单代码!如果您在 python 或 js 中有任何进一步的查询,请将其注释掉。

于 2019-10-13T04:36:36.690 回答
1
import ast,sys
input_str = sys.stdin.read()

if input_str[0] in ['a','e','i','o','u','A','E','I','O','U']:
    print('YES')
else:
    print('NO')
于 2021-06-27T08:19:58.207 回答
0

将 the_vowel 定义为字典会比列表快吗?

the_vowel = {"a":1,"e":1,"i":1,"o":1,"u":1}
my_word[0].lower() in the_vowel
于 2013-10-17T10:02:07.413 回答
0

反元音功能

def anti_vowel(text):
    vowel = ["a","e","i","o","u"]
    new_text = ''
    for char in text:
        if char.lower() in vowel:
            continue
        else:
            new_text += char
    print new_text
    return new_text
于 2015-05-21T12:14:21.567 回答
0
x = raw_input("Enter a word: ")  
vowels=['a','e','i','o','u']
for vowel in vowels:
    if vowel in x:
        print "Vowels"
    else:
        print "No vowels"

这将打印出 5 行,如果其中任何一行包含表示元音的行,则表示存在元音。我知道这不是最好的方法,但它确实有效。

于 2015-07-31T21:02:19.870 回答
0
inp = input('Enter a name to check if it starts with vowel : ') *# Here we ask for a word*

vowel = ['A','E','I','O','U', 'a','e','i','o','u']   *# This is the list of all vowels*

if inp[0] in vowel:
    print('YES')        *# Here with the if statement we check if the first alphabet is a vowel (alphabet from the list vowel)*

else:
    print('NO')     *# Here we get the response as NO if the first alphabet is not a vowel*
于 2021-09-04T16:33:11.223 回答
0

让我们以更简单的方式来做

def check_vowel(s1):
       v=['a','e','i','o','u']
       for i in v:
            if s1[0].lower()==i:
                   return (f'{s1} start with Vowel word {i}')
       else:
        return (f" {s1} start with Consonants word {s1[0]}")
 print(check_vowel("orange"))
于 2020-09-21T17:05:34.893 回答
-1
my_word = "Acrobat"
the_vowel = ["a", "e", "i", "o", "u"]

if my_word[0].lower() in the_vowel:
     print(my_word + " starts with a vowel")
else:
     print(my_word + " doesnt start with a vowel")
于 2021-07-12T14:35:49.300 回答