I'm building a pig latin translator and I can't figure out how to identify the first letter of the entered words. I've converted the input to an array with each item being a new word, but how do I select each first letter of each item to determine if it's a consonant/vowel/etc.?
a = ['This', 'is', 'a', 'sentence']
for word in a:
print(word[0])
Output:
T ias
words = ['apple', 'bike', 'cow']
Use list comprehension, that is, building a list from the contents of another:
firsts = [w[0] for w in words]
firsts
Output
['a','b','c']
using list cmprh with checking if a word not null
a = ['This', 'is', '', 'sentence']
[w[0] for w in a if w]
Output :
['T', 'i', 's']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.