Skip to content

Word Count

Background

Counting the number of words in a document is a common task in text processing and analysis. A word is typically defined as a sequence of characters separated by whitespace.

Task

Write a function word_count(s: str) -> int that takes a string and returns the number of words it contains.

Specifications

  • Input: A string s of arbitrary length.
  • Output: An integer representing the total count of words.

Constraints

  • Do not use input() or print().
  • Words are separated by any amount of whitespace (spaces, tabs, newlines).
  • An empty string or a string with only whitespace should return 0.

Example

>>> word_count("Hello World")
2
>>> word_count("  Python   is  awesome!  ")
3

Instructions

Implement the function in solution.py.

Solve Online | Leer en Español