Define a function which count vowels and consonants in a word in python

Problem: Write a Python program to count the number of vowels and consonants in a given string.

Example:

String: Pencil Programmer
Vowels: 5
Consonants: 12

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.

#define all vowels in a list
vowels = ['a', 'e', 'i', 'o', 'u']

#input a string and transform it to lower case
str = input("Enter a string: ").lower()

#define counter variable for both vowels and consonants
v_ctr = 0
c_ctr = 0

#iterate through the characters of the input string 
for x in str:
    #if character is in the vowel list,
    #update the vowel counter otherwise update consonant counter
    if x in vowels:
        v_ctr += 1
    elif x != ' ':
        c_ctr += 1

#output the values of the counters
print("Vowels: ", v_ctr)
print("Consonants: ", c_ctr)

Output:

Enter a string: Pencil Programmer
Vowels: 5
Consonants: 12

Enter a string: Python
Vowels: 1
Consonants: 5

In the above program, we have defined all vowels of the English alphabet in a list and use the same to check for vowels in the string.

Inside the for loop, we check whether the character (x) of the string is present in the vowels list or not.

If yes then it is a vowel, so we increment the vowel counter (v_ctr += 1) otherwise, we increment the counter of consonants (c_ctr += 1).

Finally, we output both counter’s values at the end of the program.

In this program, our task is to count the total number of vowels and consonants present in the given string. As we know that, The characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known as the consonant. To solve this problem, First of all, we need to convert every upper-case character in the string to lower-case so that the comparisons can be done with the lower-case vowels only not upper-case vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match each character with all the vowels, i.e., a, e, I, o, u. If the match is found, increase the value of count by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given below.

Algorithm

  1. Define a string.
  2. Convert the string to lower case so that comparisons can be reduced. Else we need to compare with capital (A, E, I, O, U).
  3. If any character in string matches with vowels (a, e, i, o, u ) then increment the vcount by 1.
  4. If any character lies between 'a' and 'z' except vowels, then increment the count for ccount by 1.
  5. Print both the counts.

Complexity

O(n)

Solution

Python

Output:

Number of vowels: 10
Number of consonants: 17

C

Output:

Number of vowels: 10
Number of consonants: 17

JAVA

Output:

Number of vowels: 10
Number of consonants: 17

C#

Output:

Number of vowels: 10
Number of consonants: 17

PHP

Output:

Number of vowels: 10
Number of consonants: 17


Define a function which count vowels and consonants in a word in python
C#, Java, Python, C++ Programaming Examples

In this article, we will discuss the concept of the Python program to count vowels or consonants of the given string

In this post, we are going to learn how to count the vowels and consonants in the given string in Python  programming language

Define a function which count vowels and consonants in a word in python

Python code to count the vowels and consonants using for loop

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using for loop in Python  language

Program 1: Write a program to count the number of vowels and consonants in a given string

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#Python program to count vowel or consonant of the given string

defletters(input):

    return''.join([cforcininput ifc.isalpha()])

str=input("Please enter a string as you wish: ");

str=letters(str)

vowels=0

consonants=0

foriinstr:

  if(i=='a'ori=='e'ori=='i'ori =='o'ori=='u'or

   i=='A'ori=='E'ori== 'I'ori=='O'ori=='U'):

      vowels=vowels+1;#vowel counter is incremented by 1

  else:

    consonants=consonants+1;

#consonant counter is incremented by 1

print("The number of vowels:",vowels);

print("\nThe number of consonant:",consonants);

When the above program is executed, it produces the following result

Pleaseenterastringasyouwish:python

Thenumberofvowels:1

Thenumberofconsonants:5

Define a function which count vowels and consonants in a word in python

Approach

  1. Declare and initialize two integer counter variable as int vowels=0 and consonants=0;
  2. The user asked to enter a string to count vowels and consonants
  3. A for-loop is used to count total vowels and consonants of the given string
  4. Use an if statement to test vowel if the test expression is true,  vowels become vowels + 1(vowels=vowels+1)
  5. When the if-statement is false, control moves to else part and executes else part statements
  6. Finally, the program displays the number of vowels and consonants of the given string.

Python code to count the vowels and consonants using for loop – strlen() function

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using strlen()

Program 2: Write a program to count the number of vowels and consonants in a given string

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#Python program to count vowel or consonant of the given string

defletters(input):

    return''.join([cforcininput ifc.isalpha()])

str=input("Please enter a string as you wish: ");

str=letters(str)

vowels=0

consonants=0

str.lower()#call the lower function to avoid upper case letter

foriinstr:

    if(i=='a'ori== 'e'ori=='i'ori=='o'ori=='u'):

           vowels=vowels+1;

    else:

        consonants=consonants+1;

print("The number of vowels:",vowels);

print("\nThe number of consonant:",consonants);

When the above program is executed, it produces the following result

Pleaseenterastringasyouwish:Pythonlanguage

Thenumberofvowels:5

Thenumberofconsonants:10

Approach

  1. Declare and initialize two integer counter variable as int vowels=0 and consonants=0;
  2. The user asked to enter a string to count vowels and consonants
  3. call the str.lower() string function to change upper case lettersto lower case letters
  4. A for-loop is used to count total vowels and consonants of the given string using the vowels and consonants variables
  5. Use an if statement to test vowel if the test expression is true,  vowels become vowels + 1(vowels=vowels+1)
  6. When the if-statement is false, control moves to else part and executes else part statements
  7. Finally, the program displays the number of vowels and consonants of the given string.

You may also like

How does python count vowels and consonants?

Python.
vcount = 0;.
ccount = 0;.
str = "This is a really simple sentence";.
#Converting entire string to lower case to reduce the comparisons..
str = str.lower();.
for i in range(0,len(str)):.
#Checks whether a character is a vowel..
if str[i] in ('a',"e","i","o","u"):.

How do you count consonants in a word in 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.

How do you count vowels and consonants in a string?

Take the string as input. Take each character from this string to check. If this character is a vowel, increment the count of vowels. Else increment the count of consonants.

How do you count the number of vowels in python?

Step 1: Take a string from the user and store it in a variable. Step 2: Initialize a count variable to 0. Step 3: Use a for loop to traverse through the characters in the string. Step 4: Use an if statement to check if the character is a vowel or not and increment the count variable if it is a vowel.