Count Words

Count number of words in string.

Logic BuildingBeginner
Logic Building
# Take string input
s = input("Enter a string: ")

# Count words
words = s.split()
count = len(words)

print(f"Number of words: {count}")

Output

Enter a string: Hello World Programming
Number of words: 3

Split string and count elements.

Key Concepts:

  • split() splits by whitespace
  • Returns list of words
  • Count list length