Vowels in a sentence as tuples in Python


This Python code takes a sentence and creates a tuple called vowels, which contains all the vowel characters from the original sentence (both lowercase and uppercase vowels are considered). 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?"
  • vowels = tuple(char for char in sentence if char.lower() in 'aeiou'): This line of code initializes a variable named vowels and assigns it a tuple created using a generator expression.
    • for char in sentence: This part sets up a loop that iterates through each character char in the sentence.
    • char.lower() in 'aeiou': For each character in the sentence, this expression converts char to lowercase using char.lower() to ensure case insensitivity and checks if the lowercase character is in the string 'aeiou', which contains all lowercase vowels.
    • tuple(...): This surrounds the generator expression and converts the generated vowel characters into a tuple.
  • print(sentence): This line of code prints the original sentence to the console.
  • print(vowels): This line of code prints the vowels tuple (which contains the vowel characters from the sentence) to the console.

Source Code

sentence = "Hello, how are you?"
vowels = tuple(char for char in sentence if char.lower() in 'aeiou')
print(sentence)
print(vowels)

Output

Hello, how are you?
('e', 'o', 'o', 'a', 'e', 'o', 'u')

Example Programs