checking if the first letter of a word is a vowel

71,950

Solution 1

try my_word[0].lower() in the_vowel

Solution 2

I don't know if it is better than the answers already posted here, but you could also do:

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

Solution 3

Here are some hints to help you figure it out.

To get a single letter from a string subscript the string.

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

Note that the first character is character zero, the second character is character one, and so forth.

The next thing to note is that an upper case letter does not compare equal to a lower case letter:

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

Luckily, python strings have the methods upper and lower to change the case of a string:

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

To test for membership in a list us in:

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

So in order to solve your problem, tie together subscripting to get a single letter, upper/lower to adjust case, and testing for membership using in.

Solution 4

my_word = "Acrobat"
the_vowel = "aeiou"

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

Solution 5

My code looks like this.

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
Share:
71,950
Bola Owoade
Author by

Bola Owoade

Updated on August 10, 2022

Comments

  • Bola Owoade
    Bola Owoade almost 2 years

    I am trying to use python to write a function that checks whether the first letter of a given word, for instance "ball" is a vowel in either uppercase or lowercase. So for instance:

    #here is a variable containing a word:
    my_word = "Acrobat"
    
    #letters in vowel as a list
    the_vowel = ["a","e","i","o","u"]
    

    How do a check that the first letter in "Acrobat" is one of the vowels in the list? I also need to take into consideration whether it is upper or lowercase?

  • user1773242
    user1773242 over 11 years
    And you can use my_word[0].isupper() to check if the letter is uppercase.
  • Gary
    Gary over 11 years
    @user1773242: Gefei's answe already forces the string to lower case before checking
  • Steven Rumbalski
    Steven Rumbalski over 11 years
    @gefei: Thanks. Now if I could only figure out why I got the -1.
  • Rohit Jain
    Rohit Jain over 11 years
    @StevenRumbalski.. Unfortunately, if there are some on SO who don't like good answers, I added a +1 to compensate the loss.. Nice answer though :)
  • Steven Rumbalski
    Steven Rumbalski over 11 years
    +1. Nice. I always forget that startswith can take a tuple and end up writing any(word.startswith(prefix) for prefix in prefixes).
  • user1773242
    user1773242 over 11 years
    Yes, but the question stated: "I also need to take into consideration whether it is upper or lowercase?" Which I read as: "I need to controll if is it upper or lower case"
  • user2417270
    user2417270 almost 11 years
    ok "if myword[0].lower() in the_vowel:" is shorter but we are learning :D
  • Matt Coubrough
    Matt Coubrough over 9 years
    This will print 1st letter is a consonant if the first letter is a punctuation mark ;)
  • PYPL
    PYPL almost 9 years
    making myWord lowercase should skip checking capital letters, thus vowels = ('a','e','i','o','u') would be enough
  • Jeroen Heier
    Jeroen Heier over 4 years
    Welcome to StackOverflow. This answer adds nothing to the already existing answers.