Text Counter
What you'll build
A Python script that reads a multi-line text from the user, then prints the number of characters, words, and lines. A miniature version of a word-count tool.
Goals
- Use len() to count characters and lines
- Use str.split() to split text into words
- Read multi-line input with a sentinel loop
- Format a summary report with f-strings
Prerequisites
- Strings and string methods
- Lists and len()
- for or while loops
Steps
- 1
Read multi-line input
Read lines from the user until they type a sentinel line ("END"). A while loop with a break condition is the standard way to read unknown-length input.
Codelines = [] print("Enter text. Type END on its own line to finish.") while True: line = input() if line == "END": break lines.append(line)Expected result
The script reads lines until the user types END, storing them in a list.
Hint
In a real script you would read from a file; here we use input for simplicity.
- 2
Join lines and count characters
Join the lines into one string with newline separators, then use len() to count characters. len() on a string returns the number of characters including spaces.
Codetext = "\n".join(lines) char_count = len(text)Expected result
char_count holds the total number of characters in the joined text.
- 3
Count words and lines
Use text.split() to split on any whitespace and len() to count the resulting words. Line count is just len(lines). split() with no argument handles multiple spaces and newlines.
Codewords = text.split() word_count = len(words) line_count = len(lines)Expected result
word_count and line_count hold the two remaining counts.
- 4
Print the summary
Print all three counts with f-strings. A clear summary is the output the user actually wants from a counting tool.
Codeprint(f"Characters: {char_count}") print(f"Words: {word_count}") print(f"Lines: {line_count}")Expected result
Three lines showing the character, word, and line counts.
Try it yourself
lines = []
print("Enter text. Type END on its own line to finish.")
# TODO: read lines, join, count chars/words/lines, print summarylines = []
print("Enter text. Type END on its own line to finish.")
while True:
line = input()
if line == "END":
break
lines.append(line)
text = "\n".join(lines)
char_count = len(text)
words = text.split()
word_count = len(words)
line_count = len(lines)
print(f"Characters: {char_count}")
print(f"Words: {word_count}")
print(f"Lines: {line_count}")What you learned
You combined a sentinel input loop, string joining, splitting, and len() to build a text analysis tool — the same string-processing patterns used by word counters, log parsers, and data scripts.
Related Resources
Related Lessons
Related Practice
- Strings Predict
Predict the result of split, join, and len on strings.
- Lists MCQ
Quiz on list operations like append and len.
Related Tools
- Word Counter
Compare your Python output with a browser-based word counter.
Related Cheatsheets
- Python Cheatsheet
String method, list, and loop syntax reference.
Related Reference
- Python String Methods
Complete string method reference.
- Python List Methods
Complete list method reference.
Related Errors
- Index Error
Accessing invalid indices.
- Attribute Error
Calling nonexistent methods.
Related Glossary
- String
Text data in code.
- List
Ordered mutable collections.
- Dictionary
Key-value mappings.
- Python
Python language concepts.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- String
Text data in code.
- List
Ordered mutable collections.
- Dictionary
Key-value mappings.
- Python
Python language concepts.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Index Error
Accessing invalid indices.
- Attribute Error
Calling nonexistent methods.