Given a sentence, the task is to print each word of a Sentence along with the number of vowels in each word.
Examples:
Example1:
Input:
Given word ='Hello this is BTechgeeks'
Output:
The total number of vowels in the given word { hello } is 2 The total number of vowels in the given word { this } is 1 The total number of vowels in the given word { is } is 1 The total number of vowels in the given word { btechgeeks } is 3
Example2:
Input:
Given word = 'good morning this is btechgeeks python coding platform'
Output:
The total number of vowels in the given word { good } is 2 The total number of vowels in the given word { morning } is 2 The total number of vowels in the given word { this } is 1 The total number of vowels in the given word { is } is 1 The total number of vowels in the given word { btechgeeks } is 3 The total number of vowels in the given word { python } is 1 The total number of vowels in the given word { coding } is 2 The total number of vowels in the given word { platform } is 2
Program to Print each Word of a Sentence along with the Number of Vowels in each Word in Python
Below are the ways to print each word of a Sentence along with the number of vowels in each word.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Method #1: Using For Loop (Static Input)
Approach:
- Give the sentence as static input and store it in a variable.
- Convert the given string/sentence to lowercase using the lower() function.
- To break words from a sentence, use the string.split() method.
- The words will be separated and kept in a list named ‘stringwords’
- Create a list called vowelchars, which will hold all of the vowels in the English alphabet.
- Iterate through the list of words and set up a counter to count the number of vowels in each one.
- Start a nested loop that iterates throughout the word in question, checking whether any of the characters are vowels.
- Increase the counter if a character is a vowel.
- Print the current iteration’s word and the value of the counter corresponding with it (which contains the sentence’s number of vowels).
- The Exit of the program.
Below is the implementation:
# Give the sentence as static input and store it in a variable. givnstrng = 'Hello this is BTechgeeks' # Convert the given string/sentence to lowercase using the lower() function. givnstrng = givnstrng.lower() # To break words from a sentence, use the string.split() method. # The words will be separated and kept in a list named ‘wordslst' wordslst = givnstrng.split() # Create a list called vowelchars, which will hold all of the vowels in the English alphabet. vowelchars = ['a', 'e', 'i', 'o', 'u'] # Iterate through the list of words and set up a counter to count # the number of vowels in each one. for gvnword in wordslst: cntvowel = 0 # Start a nested loop that iterates throughout the word in question, # checking whether any of the characters are vowels. for m in gvnword: # Increase the counter if a character is a vowel. if m in vowelchars: cntvowel = cntvowel+1 # Print the current iteration's word and the value # of the counter corresponding with it # (which contains the sentence's number of vowels). print( 'The total number of vowels in the given word {', gvnword, '} is', cntvowel)
Output:
The total number of vowels in the given word { hello } is 2 The total number of vowels in the given word { this } is 1 The total number of vowels in the given word { is } is 1 The total number of vowels in the given word { btechgeeks } is 3
Method #2: Using For Loop (User Input)
Approach:
- Give the sentence as user input using input() and store it in a variable.
- Convert the given string/sentence to lowercase using the lower() function.
- To break words from a sentence, use the string.split() method.
- The words will be separated and kept in a list named ‘stringwords’
- Create a list called vowelchars, which will hold all of the vowels in the English alphabet.
- Iterate through the list of words and set up a counter to count the number of vowels in each one.
- Start a nested loop that iterates throughout the word in question, checking whether any of the characters are vowels.
- Increase the counter if a character is a vowel.
- Print the current iteration’s word and the value of the counter corresponding with it (which contains the sentence’s number of vowels).
- The Exit of the program.
Below is the implementation:
# Give the sentence as user input using input() and store it in a variable. givnstrng = input('Enter some random sentence = ') # Convert the given string/sentence to lowercase using the lower() function. givnstrng = givnstrng.lower() # To break words from a sentence, use the string.split() method. # The words will be separated and kept in a list named ‘wordslst' wordslst = givnstrng.split() # Create a list called vowelchars, which will hold all of the vowels in the English alphabet. vowelchars = ['a', 'e', 'i', 'o', 'u'] # Iterate through the list of words and set up a counter to count # the number of vowels in each one. for gvnword in wordslst: cntvowel = 0 # Start a nested loop that iterates throughout the word in question, # checking whether any of the characters are vowels. for m in gvnword: # Increase the counter if a character is a vowel. if m in vowelchars: cntvowel = cntvowel+1 # Print the current iteration's word and the value # of the counter corresponding with it # (which contains the sentence's number of vowels). print( 'The total number of vowels in the given word {', gvnword, '} is', cntvowel)
Output:
Enter some random sentence = good morning this is btechgeeks python coding platform The total number of vowels in the given word { good } is 2 The total number of vowels in the given word { morning } is 2 The total number of vowels in the given word { this } is 1 The total number of vowels in the given word { is } is 1 The total number of vowels in the given word { btechgeeks } is 3 The total number of vowels in the given word { python } is 1 The total number of vowels in the given word { coding } is 2 The total number of vowels in the given word { platform } is 2
Related Programs:
- python program to accept a sentence and print only the first letter of each word in capital letters separated by a full stop
- java program to count the number of vowels and consonants in a sentence
- python program to count the number of digits present in a number
- python program to print binary representation of a number
- program to clear the rightmost set bit of a number in cpp and python
- python program to remove the ith occurrence of the given word in a list where words can repeat
- python program to search the number of times a particular number occurs in a list