Showing posts with label consonants and vowels in python. Show all posts
Showing posts with label consonants and vowels in python. Show all posts

Sunday, June 1, 2014

Consonants and Vowels Counter in Python

As I continue my study how to program in Python programming language I have discovered that Python has many build in functions that makes programming easier and fun. I have decided to create a program that I called Consonants and Vowels Counter what the program will do is very simple it will as the user to enter a sentence or a string and then the program itself will count the number of consonants and vowels in a given sentence or string.

In order for us to understand what are vowels and consonants let us have some little review what is a consonants. Consonants are letters that does not belong to A,E,I,O,U they are as follows B,C,D,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y and Z. One of the built in command in Python in the list command where we can list down all the letters in vowels and consonants so that later on in our program we can easily search those letters in a given string or sentence by the user. The next portion of our program it will ask the user to enter a sentence or a string using the following commands words = input("Kindly enter a sentence or a word :=> ").lower().strip() again in Python programming we are allow to use a variable in our program without declaring it and having its own data type. In this case I use the keyword words as my variable to accept sentence, words or string from our user.

The function lower() and strip() that I added in our program will convert the sentence or string into lowercase and strip any special characters in a given sentence by our user of our program. The next commands number_of_consonants = sum(words.count(c) for c in consonants) and  number_of_vowels = sum(words.count(c) for c in vowels) will count the number of vowels and consonants in a given sentence by the user of our program. I am using a function in Python called count that will count the number of occurrence of vowels and consonants that is being store in our list function. We have also here a for loop statement with variable c which connect to our consonants and vowel variables.

After the checking and counting of consonants and vowels in our program we will display the results in our screen using the following commands print("The String is" ,str(words).upper(),"."), print(""), print(" Number of  Vowels     : ",number_of_vowels),  print(" Number of  Consonants : ",number_of_consonants) the first command it will display the sentence provided by our user in this case I converted the given sentence into uppercase format after which it will display how many vowels and consonants in the given sentence by our user.

The best thing about this Consonants and Vowels Counter Program that I wrote is that I added a while loop statement to enable the user to re run again the program so that it can accept another sentence or string. It makes our program interactive and more user friendly.

I hope for find my work useful in your quest in learning how to program in Python programming language. In my own opinion as a programmer I can say writing a program in Python is much easier and shorter compared with other programming languages like C,C++,C# and Java because it has many built it function that makes the life of a programmer a breeze.

Thank you very much.


Sample Output Of Our Program


Our Python Text Editor

Program Listing

print("\n")       
print("@==============================@")
print(" Consonants and Vowels Counter")
print("@==============================@")

vowels = list("aeiou")
consonants = list("bcdfghjklmnpqrstvwxyz")

complete = False
while complete == False:
  print("")       
  words = input("Kindly enter a sentence or a word :=> ").lower().strip()
       
  number_of_consonants = sum(words.count(c) for c in consonants)
  number_of_vowels = sum(words.count(c) for c in vowels)
  
  print("")       
  print("The String is" ,str(words).upper(),".")
  print("")
  print(" Number of  Vowels     : ",number_of_vowels)
  print(" Number of  Consonants : ",number_of_consonants)
  print("")
  choice = input("Are You Finish?  Y/N: ").lower().strip()
  if choice == "y":
       complete = True
  else:
       print("")
print("\n")       
print("Thank You For Using This Program.")
print("\n")