Count Words in String

Count the number of words in a sentence.

PythonBeginner
Python
# Program to count words in a sentence

sentence = input("Enter a sentence: ")

words = sentence.split()

print("Number of words:", len(words))

Output

Enter a sentence: count the words here
Number of words: 4

We split on whitespace and use len(words) to count words.