One of our site visitors asked a question to write a program that accepts a sentence and calculate the number of letters and words in Python programming language. In this post, we have described his question and shared a python program with the output.
Let’s suppose the following input is supplied to the program:
1 2 3 4 5 | #Input Free Web Mentor #Output LETTERS 13 WORDS 3 |
1 2 3 4 5 6 7 8 9 10 11 | s = raw_input() d={"WORDS":0, "LETTERS":0} for c in s: if c.isdigit(): d["WORDS"]+=1 elif c.isalpha(): d["LETTERS"]+=1 else: pass print "LETTERS", d["LETTERS"] print "WORDS", d["WORDS"] |
To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.