Examples
- Palindrome Program in Python (Check String is Palindrome or Not)
- Program to Remove Punctuations From String in Python
- Remove a Character from String in Python (Program With Examples)
- Remove Stop Words from String in Python Using NLTK and spaCy
- Program to Sort Words in Alphabetical Order in Python (Arrange in Alphabetic Order)
- How to Sort Strings in Python? (Program With Examples)
- How to Count Vowels in Python String? Vowels Program in Python
- How to Remove Vowels from String in Python? Program With Examples
- How to Convert String to Int or Float in Python? String Parse Program
- How to Convert Float to Int in Python? Program With Examples
- How to Convert Int to String in Python? Program with Examples
- Remove Spaces from String in Python (Trim Whitespace)
- Python Program to Check If Two Strings are Anagram
- How to Capitalize First Letter in Python? String Capitalization Program
- Find All Permutations of String in Python (Programs and Examples)
- Find All Substrings of a String in Python (Programs with Examples)
- Create Multiline String in Python (With & Without New Line)
How to Count Vowels in Python String? Vowels Program in Python
The skill to count the number of vowels in a string in Python is useful in text processing and analysis. By determining the frequency of vowels in a given string, programmers can extract valuable insights and perform various linguistic operations. Whether you're working on text analytics, natural language processing, or data validation, the vowels program in Python plays a good role.
It can be used in various scenarios, including:
-
Text Analysis: Counting vowels helps analyze the linguistic properties of a given text. By determining the frequency of vowels, programmers can gain insights into the language usage, vowel distribution, or vowel patterns within the text corpus.
-
Language Processing: When working with natural language processing tasks, counting vowels in Python string assists in text preprocessing and normalization. It can be used to extract phonetic features, syllable counts, or identify words with specific vowel patterns.
-
Data Validation: Counting vowels in user input or text data allows programmers to perform data validation. For instance, it can help identify words or phrases that contain an unusually high or low number of vowels, which may indicate errors or anomalies in the data.
-
Educational Applications: Counting vowels can be utilized in educational programs or language learning tools. It helps evaluate language proficiency, assess pronunciation, or provide feedback to learners on their vowel usage.
So, here, we will understand how to count vowels in a string in Python. We will see different programs to do it, along with proper code explanations. Furthermore, you can practice these programs online using the free Python Compiler.
Program to Count Vowels in a String in Python
Here's a Python program to count the number of vowels in a given string using for loop and function:
Code
def count_vowels(string):
vowels = 'aeiouAEIOU'
count = 0
for char in string:
if char in vowels:
count += 1
return count
# Example usage
input_string = 'Tutorials Freak!'
vowel_count = count_vowels(input_string)
print("Number of vowels:", vowel_count)
Output
Number of vowels: 6
Explanation
In this program, we define a function called count_vowels that takes a string as input. We initialize a variable called count to 0, which will keep track of the number of vowels.
Next, we iterate through each character in the string using a for loop. For each character, we check if it is present in the string vowels, which contains all the vowel characters (both lowercase and uppercase). If the character is found in the vowels string, we increment the count variable by 1.
After iterating through all the characters in the string, we return the final count of vowels.
Count Vowels in Python String Using while loop
Here's a Python program to count the number of vowels in a string using a while loop:
Code
def count_vowels(string):
vowels = 'aeiouAEIOU'
count = 0
index = 0
while index < len(string):
if string[index] in vowels:
count += 1
index += 1
return count
# Example usage
input_string = 'Hello, World!'
vowel_count = count_vowels(input_string)
print("Number of vowels:", vowel_count)
Output
Number of vowels: 3
Explanation
We define a function called count_vowels that takes a string as input. We initialize a variable called count to 0, which will keep track of the number of vowels. We also initialize an index variable to 0, which represents the current index being examined in the string.
We then enter a while loop that continues until the index reaches the length of the string. Inside the loop, we check if the character at the current index, string[index], is present in the vowels string that contains all the vowel characters. If it is, we increment the count variable by 1.
After examining the character at the current index, we increment the index by 1 to move to the next character in the string.
Once the index reaches the length of the string, the while loop terminates, and we return the final count of vowels.
Python Program to Count Vowels and Consonants in String
You can use the following Python program to count the number of vowels and consonants in a string:
Code
def count_vowels_consonants(string):
vowels = 'aeiouAEIOU'
vowel_count = 0
consonant_count = 0
for char in string:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
return vowel_count, consonant_count
# Example usage
input_string = 'Tutorials Freak'
vowels, consonants = count_vowels_consonants(input_string)
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
Output
Number of vowels: 6
Number of consonants: 8
Explanation
We define the count_vowels_consonants function to take a string as input. We initialize variables vowel_count and consonant_count to 0, which will keep track of the number of vowels and consonants, respectively.
We then iterate through each character in the string using a for loop. For each character, we first check if it is alphabetic using the isalpha() method. This ensures that only letters are counted as vowels or consonants.
If the character is alphabetic, we check if it is present in the vowels string, which contains all the vowel characters. If it is, we increment the vowel_count variable by 1. Otherwise, we increment the consonant_count variable by 1.
After iterating through all the characters in the string, we return the final counts of vowels and consonants as a tuple.