Checking if the first letter of a word is a vowel

Before asking a question, I want to report that I am not an experienced programmer, but someone is learning the basics of python at codecademy.com.

I am trying to use python to write a function that checks if the first letter of a given word, such as a β€œball”, is an upper or lower case vowel. For example:

#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 I verify that the first letter in "Acrobat" is one of the vowels in the list? Should I also take this into account in upper or lower case?

+7
source share
12 answers

try my_word[0].lower() in the_vowel

+16
source

I don't know how much better this is than the answers already posted here, but you can also do:

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

Here are some tips to help you figure it out.

To get one letter from the string index of a string.

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

Note that the first character is the character zero, the second character is the character one, etc.

The next thing to note is that an uppercase letter does not compare with a lowercase letter:

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

Fortunately, python strings have upper and lower methods to change the string case:

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

Check us in :

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

So, to solve your problem, bind the subscriber to get one upper / lower letter to set up the case, and testing for membership with in .

+9
source
 my_word = "Acrobat" the_vowel = "aeiou" if myword[0].lower() in the_vowel: print('1st letter is a vowel') else: print('Not vowel') 
+4
source

My code is as follows.

 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 
+2
source
 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) 
+2
source

thanks to alexvassel who provided me with the missing part;)

so here is the exercise solution at 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' 
+1
source

This is how I did it, since the entered word must first be checked 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' 
+1
source

Would 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 
0
source

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
source
 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 will print 5 lines, if any of them has a line that says vowels, then there is a vowel. I know this is not the best way, but it works.

0
source

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 a simple code to determine if the first letter is a vowel or not !! If you have any further requests in python or js, then comment them out.

0
source

Source: https://habr.com/ru/post/1445992/


All Articles