Count vowels and consonants in python assignment expert

Write a program to count Vowels and Consonants in string.
Input

The input will be a single line containing a string s.
Output

The first line of output should contain no of Vowels in the given string
The second line of output should  contain no of Consonants in the given string
Explanation

For example, if the given string is "Good Morning"
Vowels in the string "Good Morning" are "o, i" and their count is 4.
Remaining characters in the string are consonants their count is 7.
The First line of output is 4\nThe second line of output is 7.
Sample Input 1
Good Morning
Sample Output 1
4
7

Sample Input 2
welcome
Sample Output 2
3
4

#The input will be a single line containing a string s.
inputString=input["Enter string: "]
vowels=0
consonants=0
inputString=inputString.lower[]
for l in inputString:
    #check if the letter of the string is vowel
    if[l == 'a' or l == 'e' or l == 'i' or l == 'o' or l == 'u' ]:
        vowels+=1
    else:
        #check if the letter of the string is consonant
        if l != ' ' and l.isdigit[]==False:
            consonants+=1
#The first line of output should contain no of Vowels in the given string
print["Vowels: "+str[vowels]]
#The second line of output should  contain no of Consonants in the given string
print["Consonants: "+str[consonants]]

Learn more about our help with Assignments: Python

For example, if the given string is "Good Morning"

Vowels in the string "Good Morning" are "o, i" and their count is 4.

Remaining characters in the string are consonants their count is 7.

The First line of output is 4\nThe second line of output is 7.

givenString=input[]
vowels=0
consonants=0
givenString=givenString.lower[]
for letter in givenString:
    if[letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u' ]:
        vowels+=1
    else:
        if letter != ' ' and letter.isdigit[]==False:
            consonants+=1
print[str[vowels]]
print[str[consonants]]

Learn more about our help with Assignments: Python

Python program to count vowels or consonants of the given string

Python program to count vowels or consonants of the given string

Contents

  • 1 Python program to count vowels or consonants of the given string
      • 1.0.1 Python code to count the vowels and consonants using for loop
      • 1.0.2 Python code to count the vowels and consonants using for loop – strlen[] function
    • 1.1 Suggested for you
    • 1.2 Related posts:

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

count vowels and consonant

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

#Python program to count vowel or consonant of the given string
str=input["Please enter a string as you wish: "];
vowels=0
consonants=0

for i in str:
    if[i == 'a'or i == 'e'or i == 'i'or i == 'o'or i == 'u' or
       i == 'A'or i == 'E'or i == 'I'or i == 'O'or i == '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

Please enter a string as you wish: python
The number of vowels: 1
The number of consonants: 5

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[] function in Python  language

Program 2

#Python program to count vowel or consonant of the given string
str=input["Please enter a string as you wish: "];
vowels=0
consonants=0
str.lower[]#call the lower function to avoid upper case letter
for i in str:
    if[i == 'a'or i == 'e'or i == 'i'or i == 'o'or i == '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

Please enter a string as you wish: Python language
The number of vowels: 5
The number of consonants: 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.

Suggested for you

for loop in Python

while loop in Python

if statements in Python

Operator in Python

data type in Python

Similar post

C code to check whether the Alphabet is vowel or consonant

C++ code to check whether the Alphabet is vowel or consonant

Python  code to check whether the Alphabet is vowel or consonant

Java code to check whether the Alphabet is vowel or consonant

Java program to count vowels and consonants in a string

C++ program to count vowels and consonants in a string

C program to count vowels and consonants in a string

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 the number of 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 only consonants in Python?

Python code to count the vowels and consonants using for loop – strlen[] function.
Declare and initialize two integer counter variable as int vowels=0 and consonants=0;.
The user asked to enter a string to count vowels and consonants..
call the str.lower[] string function to change upper case lettersto lower case letters..

How do you find 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.

Chủ Đề