codescracker


python

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

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 »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test