Python bootcamp · Lab 12

Count the words

easyStrings8 minLesson: String Methods

Read the question, write Python on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read one line of text and print how many words it contains.

The trick is that .split() with no argument treats any run of whitespace as a single separator, so double spaces do not fool it.

Input. One line of text.

Output. One integer: the word count.

Examples

Example 1
Input
the quick brown fox
Output
4
Example 2 — Extra spaces do not add phantom words.
Input
hello   world
Output
2
Hint
  1. input().split() returns a list of words.
  2. len(...) of that list is your answer.
Show correct code

Peek only after you have tried. You can still Check your own version.

print(len(input().split()))
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.