Saturday, 11 January 2025

Day 86: Python Program to Count Number of Vowels in a String using Sets


 def count_vowels(input_string):

    vowels = {'a', 'e', 'i', 'o', 'u'}

    input_string = input_string.lower()

    vowel_count = sum(1 for char in input_string if char in vowels)

    return vowel_count

input_string = input("Enter a string: ")

print(f"Number of vowels: {count_vowels(input_string)}")

#source code --> clcoding.com 

Code Explanation:

Defining the Function:
def count_vowels(input_string):
This defines a function called count_vowels that takes one argument, input_string. This argument will hold the string in which vowels will be counted.

2. Defining the Set of Vowels:
    vowels = {'a', 'e', 'i', 'o', 'u'}
A set named vowels is defined containing all lowercase vowels (a, e, i, o, u).
Sets are used here because they allow efficient membership checks (char in vowels).

3. Converting the Input String to Lowercase:
    input_string = input_string.lower()
The input_string is converted to lowercase using the .lower() method to handle both uppercase and lowercase vowels uniformly. For example, "A" will be treated as "a".

4. Counting the Vowels:
    vowel_count = sum(1 for char in input_string if char in vowels)
A generator expression is used to iterate through each character in the input_string.
For each character, it checks if the character exists in the vowels set.
If the character is a vowel, 1 is added to the sum.
The sum function computes the total count of vowels in the string.

5. Returning the Count:
    return vowel_count
The function returns the total number of vowels found in the input string.

6. Getting User Input and Printing the Result:
input_string = input("Enter a string: ")
print(f"Number of vowels: {count_vowels(input_string)}")
The user is prompted to enter a string using the input() function.
The entered string is passed as an argument to the count_vowels function.
The result (number of vowels) is printed to the console using an f-string for formatting.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (83) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (953) Python Coding Challenge (398) Python Quiz (53) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses