Skip to content

Text Counter

Easy~15 min

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. 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.

    Code
    lines = []
    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.

  2. 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.

    Code
    text = "\n".join(lines)
    char_count = len(text)

    Expected result

    char_count holds the total number of characters in the joined text.

  3. 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.

    Code
    words = text.split()
    word_count = len(words)
    line_count = len(lines)

    Expected result

    word_count and line_count hold the two remaining counts.

  4. 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.

    Code
    print(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

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

  • Strings

    String methods like split and join used throughout.

  • Lists

    The lines list and the words list from split().

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

Related Reference

Related Errors

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Related Errors

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

← Back to topic