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.
Nội dung chính
- Recommended: Please try your approach on {IDE} first, before moving on to the solution.
- How do you count vowels and consonants in Python?
- How do you count the number of vowels?
- How do you count letters in a string in Python?
- How do you extract a vowel from a string in Python?
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]]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a string, count the number of vowels present in given string using Sets.
Prerequisite: Sets in Python
Examples:
Input : GeeksforGeeks Output : No. of vowels : 5 Input : Hello World Output : No. of vowels : 3
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach:
1.
Create a set of vowels using set[] and initialize a count variable to 0.
2. Traverse through the alphabets in the string and check if the letter in the string is present in set vowel.
3. If it is present, the vowel count is incremented.
Below is the implementation of above approach:
def
vowel_count[
str
]:
count
=
0
vowel
=
set
[
"aeiouAEIOU"
]
for
alphabet
in
str
:
if
alphabet
in
vowel:
count
=
count
+
1
print
[
"No. of vowels :"
, count]
str
=
"GeeksforGeeks"
vowel_count[
str
]
Output:
No. of vowels : 5
This is a Python Program to count the number of vowels in a string.
Problem Description
The program takes a string and counts the number of vowels in a string.
Problem Solution
1. Take a string from the user and store it in a variable.
2. Initialize a count variable to 0.
3. Use a for loop to traverse through the characters in the string.
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.
5. Print the total number of vowels
in the string.
6. Exit.
Program/Source Code
Here is source code of the Python Program to remove the nth index character from a non-empty string. The program output is also shown below.
string=raw_input["Enter string:"] vowels=0 for i in string: 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 print["Number of vowels are:"] print[vowels]
Program Explanation
1. User must enter a string and store it in a variable.
2. The count variable is initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. An if statement checks if the character is a vowel or not.
5. The
count is incremented each time a vowel is encountered.
6. The total count of vowels in the string is printed.
Runtime Test Cases
Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 3
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.
How do you count vowels and consonants in Python?
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?
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 letters in a string in Python?
In Python, you can get the length of a string str [= number of characters] with the built-in function len[] .
How do you extract a vowel from a string in Python?
Python program to remove the vowels from a string.
Method 1 – Simple iteration on string using for loop..
Method 2 – Iterate on individual char of string..
Method 3 – Uses replace method..
Method 4 – Longer but more clear implementation..
Method 5 – One line iterator and join method..
Method 6 – Using regex..