Tuple of distinct words starting with vowels in a sentence in Python


This Python code creates a tuple called vowel_start_words, which contains words from the sentence string that start with a lowercase vowel. Here's how the code works:

  • sentence = "Hello, how are you?": This line initializes a variable named sentence and assigns it the string "Hello, how are you?"
  • vowel_start_words = tuple(word for word in sentence.split() if word[0].lower() in 'aeiou'): This line of code initializes a variable named vowel_start_words and assigns it a tuple created using a generator expression.
    • for word in sentence.split(): This part of the code sets up a loop that iterates through each word word in the sentence, split by spaces using sentence.split().
    • if word[0].lower() in 'aeiou': For each word in the split sentence, it checks if the lowercase version of the first character of the word, obtained using word[0].lower(), is in the string 'aeiou', effectively identifying words that start with a lowercase vowel.
    • word: If a word starts with a lowercase vowel, it includes the word in the generator expression.
    • tuple(...): This surrounds the generator expression and converts the generated words into a tuple.
  • print(sentence): This line of code prints the original sentence to the console.
  • print(vowel_start_words): This line of code prints the vowel_start_words tuple (which contains words that start with a lowercase vowel) to the console.

Source Code

sentence = "Hello, how are you?"
vowel_start_words = tuple(word for word in sentence.split() if word[0].lower() in 'aeiou')
print(sentence)
print(vowel_start_words)

Output

Hello, how are you?
('are',)

Example Programs