How can I check the first letter of each item in an array?
10,096
Solution 1
a = ['This', 'is', 'a', 'sentence']
for word in a:
print(word[0])
Output:
T
i
a
s
Solution 2
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']
Author by
mackmmiller
Updated on July 13, 2022Comments
-
mackmmiller almost 2 years
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.?