- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count and display vowels in a string in Python
Given a string of characters let's analyse how many of the characters are vowels.
With set
We first find out all the individual and unique characters and then test if they are present in the string representing the vowels.
Example
stringA = "Tutorialspoint is best" print("Given String: \n",stringA) vowels = "AaEeIiOoUu" # Get vowels res = set([each for each in stringA if each in vowels]) print("The vlowels present in the string:\n ",res)
Output
Running the above code gives us the following result −
Given String: Tutorialspoint is best The vlowels present in the string: {'e', 'i', 'a', 'o', 'u'}
with fromkeys
This function enables to extract the vowels form the string by treating it as a dictionary.
Example
stringA = "Tutorialspoint is best" #ignore cases stringA = stringA.casefold() vowels = "aeiou" def vowel_count(string, vowels): # Take dictionary key as a vowel count = {}.fromkeys(vowels, 0) # To count the vowels for v in string: if v in count: # Increasing count for each occurence count[v] += 1 return count print("Given String: \n", stringA) print ("The count of vlowels in the string:\n ",vowel_count(stringA, vowels))
Output
Running the above code gives us the following result −
Given String: tutorialspoint is best The count of vlowels in the string: {'a': 1, 'e': 1, 'i': 3, 'o': 2, 'u': 1}
- Related Articles
- Java Program to count vowels in a string
- C# Program to count vowels in a string
- How to Count the Number of Vowels in a string using Python?
- Java Program to count all vowels in a string
- Python program to count number of vowels using set in a given string
- C# Program to count number of Vowels and Consonants in a string
- C++ Program to count Vowels in a string using Pointer?
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Remove Vowels from a String in Python
- Reverse Vowels of a String in Python
- To count Vowels in a string using Pointer in C++ Program
- How to count number of vowels and consonants in a string in C Language?
- Count the pairs of vowels in the given string in C++
- Count Vowels Permutation in C++

Advertisements