w3resource

Python: Count and display the vowels of a given text

Python String: Exercise-49 with Solution

Write a Python program to count and display vowels in text.

Python String Exercises: Count and display the vowels of a given text

Sample Solution:

Python Code:

# Define a function 'vowel' that takes a string 'text' as input.
def vowel(text):
    # Define a string 'vowels' containing all lowercase and uppercase vowels.
    vowels = "aeiuoAEIOU"

    # Print the number of vowels in the input string 'text' using a list comprehension.
    print(len([letter for letter in text if letter in vowels]))

    # Print a list of vowels found in the input string 'text' using a list comprehension.
    print([letter for letter in text if letter in vowels])

# Call the 'vowel' function with the input string 'w3resource'.
vowel('w3resource') 

Sample Output:

4                                                                                                             
['e', 'o', 'u', 'e']               

Flowchart:

Flowchart: Count and display the vowels of a given text

Python Code Editor:

Previous: Write a Python program to swap comma and dot in a string.
Next: Write a Python program to split a string on the last occurrence of the delimiter.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.