11

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?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Bola Owoade
  • 119
  • 1
  • 1
  • 4

17 Answers17

21

try my_word[0].lower() in the_vowel

gefei
  • 18,922
  • 9
  • 50
  • 67
  • And you can use my_word[0].isupper() to check if the letter is uppercase. – user1773242 Nov 14 '12 at 13:00
  • 1
    @user1773242: Gefei's answe already forces the string to lower case before checking – Gary Nov 14 '12 at 13:12
  • 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" – user1773242 Nov 14 '12 at 14:08
17

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)
Matt
  • 3,651
  • 3
  • 16
  • 35
  • 1
    +1. Nice. I always forget that `startswith` can take a tuple and end up writing `any(word.startswith(prefix) for prefix in prefixes)`. – Steven Rumbalski Nov 14 '12 at 13:22
  • making `myWord` lowercase should skip checking capital letters, thus `vowels = ('a','e','i','o','u')` would be enough – PYPL May 21 '15 at 12:23
11

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.

Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
  • @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 :) – Rohit Jain Nov 14 '12 at 13:18
4
my_word = "Acrobat"
the_vowel = "aeiou"

if myword[0].lower() in the_vowel:
    print('1st letter is a vowel')
else:
    print('Not vowel')
alexvassel
  • 10,600
  • 2
  • 29
  • 31
2

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
Mtech
  • 584
  • 4
  • 4
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)     
Matt Coubrough
  • 3,739
  • 2
  • 26
  • 40
1

Here is the solution to the exercise on 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'
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
1

Here's how I did it since the inputted word needs to be checked first before storing it as a variable:

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'
Ali
  • 11
  • 1
1

changes:

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

So, Here is the simple code for finding out that the first letter is either vowel or not!! If you have any further query in python or js, then comment it down.

Rudra shah
  • 804
  • 8
  • 10
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')
Anton Krug
  • 1,555
  • 2
  • 19
  • 32
0

Will it not be (slightly) faster to define the_vowel as a dictionary than a list?

the_vowel = {"a":1,"e":1,"i":1,"o":1,"u":1}
my_word[0].lower() in the_vowel
HongboZhu
  • 4,442
  • 3
  • 27
  • 33
0

anti vowel Function

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

This would print out 5 lines, if any of those includes a line that says Vowels then that means there is a vowel. I know this isn't the best way but it works.

Zulu
  • 8,765
  • 9
  • 49
  • 56
0

Let's do it in more simply way

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"))
Ali A
  • 359
  • 3
  • 5
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*
Mohana B C
  • 5,021
  • 1
  • 9
  • 28
-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")
Josef
  • 2,869
  • 2
  • 22
  • 23
-1
input_str="aeroplane" 

if input_str[0].lower() in ['a','e','i','o','u']:

    print('YES')

else:

    print('NO')

Output will be YES as the input string starts with a here.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Please read [answer]. This question is nearly 10 years old and already has several existing answers, including [ones that already](https://stackoverflow.com/a/13379284/354577) [give this solution](https://stackoverflow.com/a/13379266/354577). Please don't repeat answers. – ChrisGPT was on strike Jun 19 '22 at 16:03