Educatifu
Open menu

Word Count

Easystrings

Count the words on a line. Words are separated by one or more spaces.

Input

A single line of text.

Output

The number of whitespace-separated words.

Examples

Example 1

Input
the quick brown fox
Output
4

Four words.

Example 2

Input
hello
Output
1

One word.

Example 3

Input
a b  c
Output
3

Repeated spaces still separate just three words.

Constraints

  • The line has at least one word and at most 10^4 characters.

Hints

Reveal hint 1

Splitting the line on whitespace gives you the list of words — its length is the answer.

Reveal hint 2

Most languages ignore leading, trailing and repeated spaces when splitting on whitespace.

Reference solution (spoiler — try it yourself first)

Approach

Split the line on runs of whitespace and count the resulting words.

print(len(input().split()))

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation