【问题标题】:checking if the first letter of a word is a vowel检查单词的第一个字母是否是元音
【发布时间】:2012-11-02 23:22:27
【问题描述】:

我正在尝试使用 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”中的第一个字母是否是列表中的元音之一?我还需要考虑它是大写还是小写?

【问题讨论】:

  • 感谢所有回答我问题的人。您的回答帮助我回答了这个问题。

标签: python list search


【解决方案1】:

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

【讨论】:

  • 你可以使用 my_word[0].isupper() 检查字母是否为大写。
  • @user1773242:格飞的回答在检查之前已经强制字符串小写
  • 是的,但问题是:“我还需要考虑它是大写还是小写?”我读为:“我需要控制它是大写还是小写”
【解决方案2】:

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

vowels = ('a','e','i','o','u','A','E','I','O','U')
myWord.startswith(vowels)

【讨论】:

  • +1。好的。我总是忘记startswith 可以接受一个元组并最终写成any(word.startswith(prefix) for prefix in prefixes)
  • 使myWord小写应该跳过检查大写字母,因此vowels = ('a','e','i','o','u')就足够了
【解决方案3】:

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

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

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

注意第一个字符是字符 0,第二个字符是字符 1,以此类推。

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

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

幸运的是,python 字符串有 upperlower 方法来改变字符串的大小写:

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

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

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

因此,为了解决您的问题,将下标捆绑在一起以获得单个字母,upper/lower 调整大小写,并使用in 测试成员资格。

【讨论】:

  • @gefei:谢谢。现在,如果我能弄清楚为什么我得到了 -1。
  • @StevenRumbalski .. 不幸的是,如果 SO 上有些人不喜欢好的答案,我加了一个 +1 来弥补损失。虽然答案很好 :)
【解决方案4】:
my_word = "Acrobat"
the_vowel = "aeiou"

if myword[0].lower() in the_vowel:
    print('1st letter is a vowel')
else:
    print('Not vowel')

【讨论】:

    【解决方案5】:

    我的代码如下所示。

    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
    

    【讨论】:

      【解决方案6】:
      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)     
      

      【讨论】:

      • 如果第一个字母是标点符号,这将打印1st letter is a consonant ;)
      【解决方案7】:

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

      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'
      

      【讨论】:

        【解决方案8】:

        变化:

        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 中有任何进一步的查询,然后将其注释掉。

        【讨论】:

        • 欢迎来到 StackOverflow。此答案不会对现有答案增加任何内容。
        【解决方案9】:
        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')
        

        【讨论】:

          【解决方案10】:

          下面是 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'
          

          【讨论】:

          • ok "if myword[0].lower() in the_vowel:" 更短,但我们正在学习:D
          【解决方案11】:

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

          the_vowel = {"a":1,"e":1,"i":1,"o":1,"u":1}
          my_word[0].lower() in the_vowel
          

          【讨论】:

            【解决方案12】:

            反元音功能

            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
            

            【讨论】:

              【解决方案13】:
              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 行,如果其中任何一行包含表示元音的行,则表示存在元音。我知道这不是最好的方法,但它确实有效。

              【讨论】:

                【解决方案14】:

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

                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"))
                

                【讨论】:

                  【解决方案15】:
                  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*
                  

                  【讨论】:

                    【解决方案16】:
                    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")
                    

                    【讨论】:

                      猜你喜欢
                      • 2023-02-03
                      • 1970-01-01
                      • 2018-07-09
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-03-28
                      • 2021-12-07
                      • 1970-01-01
                      • 2022-11-17
                      相关资源
                      最近更新 更多