Python Program to Count Word in Sentence

Python Program to Count Word in Sentence

  • 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. 

Source Code:

# 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) 

Output:

  • Number of words = 3
  • Number of words = 31


 


Comments