We are given astring and our task is to count the number of vowels present in it. Vowels in English include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). Using sets, we can efficiently check for vowel presence due to their fast membership lookup.For example, if the input string is "Beautiful Day" then the output should be 6 as the vowels present are 'e', 'a', 'u', 'i', 'a', and 'y'.
Aset allows quick membership testing, making it an efficient way to count vowels. We iterate through the string and check if each character is in the set of vowels.
s="Python Programming"vowels={'a','e','i','o','u','A','E','I','O','U'}c=sum(1forchinsifchinvowels)print("Number of vowels:",c)
Number of vowels: 4
Explanation:
Instead of iterating through the string, we use set intersection to find common elements between the string and the vowel set.
s="Set operations in Python"vowels={'a','e','i','o','u','A','E','I','O','U'}c=sum(s.count(v)forvinset(s)&vowels)print("Number of vowels:",c)
Number of vowels: 8
S
Python Introduction
Input and Output in Python
Python Variables
Python Operators
Python Keywords
Python Data Types
Conditional Statements in Python
Loops in Python - For, While and Nested Loops
Python Functions
Recursion in Python
Python Lambda Functions
Python String
Python Lists
Python Tuples
Python Dictionary
Python Sets
Python Arrays
List Comprehension in Python
Python OOP Concepts
Python Exception Handling
File Handling in Python
Python Database Tutorial
Python MongoDB Tutorial
Python MySQL
Python Packages
Python Modules
Python DSA Libraries
List of Python GUI Library and Packages
NumPy Tutorial - Python Library
Pandas Tutorial
Matplotlib Tutorial
Python Seaborn Tutorial
StatsModel Library - Tutorial
Learning Model Building in Scikit-learn
TensorFlow Tutorial
PyTorch Tutorial
Flask Tutorial
Django Tutorial | Learn Django Framework
Django ORM - Inserting, Updating & Deleting Data
Templating With Jinja2 in Flask
Django Templates
Python | Build a REST API using Flask
How to Create a basic API using Django Rest Framework ?
Python Quiz
Python Coding Practice
Python Interview Questions and Answers