w3resource

Python: Count and display the vowels of a given text


Count and display vowels in text.

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

For more Practice: Solve these Related Problems:

  • Write a Python program to count the number of each vowel in a string and display the counts in a formatted manner.
  • Write a Python program to iterate over a text and tally vowels using a dictionary, then print each vowel with its frequency.
  • Write a Python program to use regular expressions to find and count vowels in a string and output the results.
  • Write a Python program to implement a function that returns a sorted list of vowels with their counts from an input 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.