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 while True: print("Enter 'x' for exit.") string = input("Enter any string: ") if string == 'x': break else: word_length = len(string.split()) print("Number of words =",word_length,"\n")
Here is the sample run of the above Python program illustrating how to count word in sentence:
You may also like to learn or practice the same program in other popular programming languages: