Print each word of a sentence along with number of vowels in each word using Python

In this Python tutorial, you will learn how to separate each word from a sentence in Python and then calculate the number of vowels in each word.

We shall be using certain string functions in Python like split() and lower()

 

The approach that we shall take

  1. string.lower() to convert all the characters of the given string to their respective lower case.
  2. string.split() method to separate words from a given sentence.
  3. After we separate the words, it will be stored in a list called ‘words’.
  4. Initialise a list called vowels that will contain all the vowels present in the English alphabet.
  5. Iterate over the list words and initialise a counter that will count the number of vowels present in the word.
  6. Start a nested loop that iterates over the word in question and check whether any character present in the word is a vowel or not
  7. If a character is a vowel, increase the counter.
  8. Print the word pertaining to the current iteration and the value of the counter associated with it(which contains the number of vowels in the sentence.
  9. Keep on iterating till we have reached the end of the list words.

lower() function in Python

The lower function in Python is used to convert all the characters in string to lower case.

How does the lower function in Python work?

#Initialising some strings 
sentence1 = "The Sun Rises In THE EAST" 
sentence2 = "CODING in PYTHON is fun" 
sentence3 = "CODESPEEDY is a great website" 
sentence4 = "STRINGS are FUN to work with"
#prining the original sentences
print("The original strings are:-")
print(sentence1)
print(sentence2)
print(sentence3)
print(sentence4)
#printing the words of the sentences after converting them to lower case
print("After applying lower() function:-")
print(sentence1.lower())
print(sentence2.lower())
print(sentence3.lower())
print(sentence4.lower())

Output:-

The original strings are:-
The Sun Rises In THE EAST
CODING in PYTHON is fun
CODESPEEDY is a great website
STRINGS are FUN to work with
After applying lower() function:-
the sun rises in the east
coding in python is fun
codespeedy is a great website
strings are fun to work with

We can see that the lower() function in Python has converted words like ‘PYTHON’, ‘STRINGS’ to ‘python’ and ‘strings’ respectively.

We shall use this because the vowels list that we shall initialise later contains the vowels in lower case.

 

split() method in Python

split() method in Python breaks up a sentence into its constituent words on the basis of a particular separator. Here we are separating on the basis of the spaces in between the words.

How does the split() method in Python work?

#Initialising some strings
sentence1 = "sun rises in the east"
sentence2 = "coding in python is fun"
sentence3 = "codespeedy is a great website"
sentence4 = "strings are fun to work with"
#using the split function
words1 = sentence1.split()
words2 = sentence2.split()
words3 = sentence3.split()
words4 = sentence4.split()
#printing the words of the sentences after splitting them
print("The words of the first sentence are::", words1)
print("The words of the second sentence are::", words2)
print("The words of the third sentence are::", words3)
print("The words of the fourth sentence are::", words4)

Let’s look at the output:-

The words of the first sentence are:: ['sun', 'rises', 'in', 'the', 'east']
The words of the second sentence are:: ['coding', 'in', 'python', 'is', 'fun']
The words of the third sentence are:: ['codespeedy', 'is', 'a', 'great', 'website']
The words of the fourth sentence are:: ['strings', 'are', 'fun', 'to', 'work', 'with']

Here, Python has this facility via the split() function where we are getting a separate list based on the placement of whitespaces in between words.

 

Code and Output in Python

Study the code in Python given below and try to associate it with the approach mentioned above:-

s = "Python is a fun language and I really love coding in it" 
s = s.lower()
words = s.split() 
vowels = ['a','e','i','o','u'] 
for word in words: 
c = 0 
for i in range(0,len(word)): 
if word[i] in vowels: 
c+=1 
print(f"The number of vowels in the word '{word}'' is {c}")

The output for the code in Python given above is:-

The number of vowels in the word 'python'' is 1
The number of vowels in the word 'is'' is 1
The number of vowels in the word 'a'' is 1
The number of vowels in the word 'fun'' is 1
The number of vowels in the word 'language'' is 4
The number of vowels in the word 'and'' is 1
The number of vowels in the word 'i'' is 1
The number of vowels in the word 'really'' is 2
The number of vowels in the word 'love'' is 2
The number of vowels in the word 'coding'' is 2
The number of vowels in the word 'in'' is 1
The number of vowels in the word 'it'' is 1

Explanation of the Python code:-

  • Convert all the characters in sentence to lower case using lower() function in Python.
  • Split sentence up into its constituent words. We do so using the split() function  in Python which separates all the words from the string(‘sentence’) and stores it in a list (‘words’).
  • Then initialise a list which contains all the vowels in the English alphabet [‘a’,’e’,’i’,’o’,’u’] so that we can check if the extracted character from the words of a sentence is a vowel or not.
  • Iterate over the list words and then iterate over the string of the list words. We are nesting two for loops here.
  • Initialise a counter and set the initial value to 0 in the outer loop
  • In the inner loop, we compare every character of the word with the list vowels, hence checking if the character in question is a vowel or not.
  • If the character is a vowel, we add 1 to the counter, hence counting the total number of vowels in the word that we are iterating against.
  • When the inner loop is executed, print the word and the number of vowels in it.
  • This process continues till all the elements of the list words are exhausted and we have essentially checked whether every character of every word is a vowel or not

I hope this Python tutorial was helpful!!

Leave a Reply

Your email address will not be published. Required fields are marked *