Generate a set of words that start with a vowel from a sentence
In this Python code, you have a sentence, and you want to create a set called vowel_start_words that contains words from the sentence that start with a vowel (either uppercase or lowercase). Here's how the code works:
- sentence = "This is a sample sentence with words starting with vowels.": This line defines a string called sentence containing the input sentence.
- vowel_start_words = {word for word in sentence.split() if word[0].lower() in 'aeiou'}: This line uses a set comprehension to create a set called vowel_start_words. Here's how it works:
- sentence.split() splits the sentence string into a list of words.
- for word in sentence.split() iterates through each word in the list.
- if word[0].lower() in 'aeiou' checks if the lowercase of the first character of the word is a vowel. If it is, the word is included in the set.
- print(sentence) : This line prints the original sentence to the console.
- print(vowel_start_words): This line prints the set vowel_start_words, which contains the words from the sentence that start with vowels, to the console.
Source Code
sentence = "This is a sample sentence with words starting with vowels."
vowel_start_words = {word for word in sentence.split() if word[0].lower() in 'aeiou'}
print(sentence)
print(vowel_start_words)
Output
This is a sample sentence with words starting with vowels.
{'a', 'is'}