To count total number of word present in the sentence in python, you have to ask from user to enter a sentence or string to count the total number of words as shown in the program given here.
Following python program ask from user to enter a string or sentence and count all the words that are used to form that string:
# Python Program - Count Word in Sentence print("Enter 'x' for exit."); string = input("Enter any string to count word: "); if string == 'x': exit(); else: word_length = len(string.split()); print("\nNumber of words =",word_length);
Here is the sample run of the above Python program illustrating how to count word in sentence:
Now enter any string or sentence, say i am what i am and press enter key to find out how many word the entered string contains. Here are the list of words that the given string contains:
There are 5 words present in the above string, therefore here is the sample run according to this example:
Same program on python shell:
You may also like to learn or practice the same program in other popular programming languages: