Report the size of a line of text.
What to build
A short program that splits text and measures the pieces.
Requirements
- Read a line.
- Print
words W. - Print
letters L, which counts all the characters except spaces.
Input
One line of text.
Output
Two lines.
Constraints
The line has at least one word. There can be several spaces between words.
Example
Input:
the quick fox
Output:
words 3
letters 11
Why it works
split() copes with any number of spaces, so the number of words is simply the length of the result.
Examples
Example 1 · Input
the quick fox
Expected output
words 3 letters 11
Example 2 · Input
hello world
Expected output
words 2 letters 10
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
split() with no argument gives the words.
Hint 2
Add up the length of each word, because spaces do not count.
Solution
Show one correct solution (try it yourself first)
line = input()
words = line.split()
print("words", len(words))
print("letters", sum(len(word) for word in words))