
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Vowels Using Set in a Given String - Python Program
We will count the number of vowels using set in a given string. Let's say we have the following input ?
jackofalltrades
The output should be the following, counting the number of vowels ?
65
Count the number of vowels using set in a given string
We will count the number of vowels using set in a give string ?
Example
def vowelFunc(str): c = 0 # Create a set of vowels s="aeiouAEIOU" v = set(s) # Loop to traverse the alphabet in the given string for alpha in str: # If alphabet is present # in set vowel if alpha in v: c = c + 1 print("Count of Vowels = ", c) # Driver code str = input("Enter the string = ") vowelFunc(str)
Output
Enter the string = howareyou Count of Vowels = 5
Count the number of vowels using set in a given string without using a function
We will count the number of vowels using set without using a function ?
Example
# string to be checked myStr = "masterofnone" count = 0 print("Our String = ",myStr) # Vowel Set vowels = set("aeiouAEIOU") # Loop through, check and count the vowels for alpha in myStr: if alpha in vowels: count += 1 print("Count of Vowels = ",count)
Output
Our String = masterofnone Count of Vowels = 5
Advertisements