How do you count vowels from a string in python?

This works for me and also counts the consonants as well (think of it as a bonus) however, if you really don't want the consonant count all you have to do is delete the last for loop and the last variable at the top.

Her is the python code:

data = input('Please give me a string: ')
data = data.lower()
vowels = ['a','e','i','o','u']
consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
vowelCount = 0
consonantCount = 0


for string in data:
    for i in vowels:
        if string == i:
            vowelCount += 1
    for i in consonants:
        if string == i:
            consonantCount += 1

print('Your string contains %s vowels and %s consonants.' %(vowelCount, consonantCount))

In this program, we need to count the number of vowels present in a string and display those vowels. This can be done using various methods. In this article, we will go through few of the popular methods to do this in an efficient manner. 
Examples: 
 

In a simple way
Input : Geeks for Geeks
Output :
5
['e', 'e', 'o', 'e', 'e']

This is in a different way
Input : Geeks for Geeks
Output : {'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}

Counting vowels: String Way

In this method, we will store all the vowels in a string and then pick every character from the enquired string and check whether it is in the vowel string or not. The vowel string consists of all the vowels with both cases since we are not ignoring the cases here. If the vowel is encountered then count gets incremented and stored in a list and finally printed. 
 

Python3

def Check_Vow(string, vowels):

    final = [each for each in string if each in vowels]

    print(len(final))

    print(final)

string = "Geeks for Geeks"

vowels = "AaEeIiOoUu"

Check_Vow(string, vowels);

Output: 
 

5
['e', 'e', 'o', 'e', 'e']

Counting vowels: Dictionary Way

This also performs the same task but in a different way. In this method, we form a dictionary with the vowels and increment them when a vowel is encountered. In this method, we use the case fold method to ignore the cases, following which we form a dictionary of vowels with the key as a vowel. This is a better and efficient way to check and find the number of each vowel present in a string. 
 

Python3

def Check_Vow(string, vowels):

    string = string.casefold()

    count = {}.fromkeys(vowels, 0)

    for character in string:

        if character in count:

            count[character] += 1   

    return count

vowels = 'aeiou'

string = "Geeks for Geeks"

print (Check_Vow(string, vowels))

Output: 
 

{'u': 0, 'o': 1, 'e': 4, 'a': 0, 'i': 0}

 Counting vowels: regex way 

We can also use this method to perform this task. We can use the regular expression to perform this task. We use re.findall() method to find all the vowels in string make list with them. We use len on list to find total vowels in string. 

Python3

import re

def Check_Vow(string, vowels):

    str_list = re.findall(f'[{vowels}]', string, re.I)

    print(len(str_list))

    return str_list

vowels = 'aeiou'

string = "Geeks for Geeks"

print (Check_Vow(string, vowels))

Output:

5
['e', 'e', 'o', 'e', 'e'] 

How do you count the vowels in a string?

To count the number of vowels in a given sentence:.
Read a sentence from the user..
Create a variable (count) initialize it with 0;.
Compare each character in the sentence with the characters {'a', 'e', 'i', 'o', 'u' }.
If a match occurs increment the count..
Finally print count..

How do you count the number of vowels and consonants in a given string Python?

To count the number of vowels and consonants in a string, we iterate using a for loop through each character of the string and check if it matches a vowel. If yes then, we increment the vowel counter otherwise increment the consonant counter.