Count Words

Easy · rating 950 · strings

Read a single line. Print the number of whitespace-separated words it contains.

Editorial

Count Words returns the number of whitespace-separated words in a line. Splitting on whitespace — which collapses runs of spaces — and taking the length is the cleanest approach.

print(len(line.split()))

Complexity

Time: O(n). Space: O(n).

Open Count Words in Code Arena →