All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Count Vowels In A String Python

Last Updated : Mar 11, 2024

How To Count Vowels In A String Python

In this article we will show you the solution of how to count vowels in a string python, a user may utilize a python vowel counter to determine how many vowel each word in a string, dictionary, or loop has used.

Using recursive function, one may determine the number of vowels and their lengths in a given string.

We use the function repeatedly up until the base condition. Another method to count and display the vowels in a string involves combining the filter function with the Counter class from the collection’s module.

Now let's talk about the idea of counting vowels in a Python string.

Step By Step Guide On How To Count Vowels In A String Python :-

Method - To determine the number of vowel in the given string, use list comprehension:

def vowel_count(str):
    vowels = "aeiouAEIOU"
    count = len([char for char in str if char in vowels])
    print("No. of vowels :", count)
str = "Talkerscode"
vowel_count(str)
  1. Here, you can see how the list comprehension technique is used to construct the Python code of counting vowels in a string.
  2. The function is defined on the initial line of code, and then a string containing every vowel is created.
  3. The next step is to run through the given string of characters using list comprehension to determine whether or not each character is a vowel.
  4. Add the character to a list if it is a vowel.
  5. To determine the number of vowels in the provided string, just use len() method to count the elements in the list.
  6. Next we output the number of vowel in the string using the print function from Python.

Method - Using reduce() and lambda function:

from functools import reduce
 def check_vow(string, vowels):
    vowel_list = reduce(lambda x, y: x + [y] if y in vowels else x, string, [])
    print(len(vowel_list))
    print(vowel_list)
 string = "Talkerscode"
vowels = "AaEeIiOoUu"
check_vow(string, vowels)
  1. In this example, you can see how the lambda function and reduction method are used to construct the Python code to count the vowels in a string.
  2. Our code begins with the import of the reduction module.
  3. Python's reduction() function, which is included in the functools module, does not return an iterator or a list of values in more than one place.
  4. It just outputs a single value, which is the outcome of reducing the entire iterable to just a single integer, string, or boolean.
  5. Following that, we write a function called "check vow" that accepts a string or a string that contains all vowels as inputs.
  6. Make a list of the vowels in the provided string using the reduce function.
  7. Print the list of vowel's length as well as the list of vowel after that.
  8. And use the provided string & vowel string to call the function.

Conclusion :-

Hence we have successfully learned the concept of how to count vowels in a string python.

We also learned that a user can use a Python vowels counter to see how many vowels every word in a string, dictionaries, and loop has already used.

I hope this article on how to count vowels in a string python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪