Sunday, February 21, 2016

Count Number of Words in a Sentence in Python

A sample program that I wrote using Python programming language that will count the number of words in a given sentence by the user. The code is very short and very easy to understand. I hope you will find my work useful in learning Python programming.

If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.


Program Listing

import re

def wordcounter(words):
    list = re.findall("(\S+)", words)
    return len(list)

print("\n");
print("\t Word Counter in Python");
print("\n");
str = input('Give a sentence : ')
print("\n");
print("Total Numbers of Words is",wordcounter(str),'.')
print("\n");
print("End of Program")
print("\n");

No comments:

Post a Comment