Python Program to Count Word in Sentence

« Previous Program Next Program »

Count Word in Sentence in Python

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.

Python Programming Code to Count Word in Sentence

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':
    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:

count word in string python

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:

count word python

Same program on python shell:

python count word in sentence

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:

« Previous Program Next Program »


Quick Links
Signup - Login - Give Online Test