AI Skill Report Card

Wrapping and Formatting Text

A89·Aug 14, 2026·Source: Web
YAML
--- name: wrapping-and-formatting-text description: Wraps, fills, shortens, and dedents plain text using Python's textwrap module. Use when formatting long strings into readable paragraphs, wrapping CLI output, truncating text with ellipsis, removing common leading whitespace from multi-line strings, or indenting text blocks. ---

Wrapping and Formatting Text

Uses Python's built-in textwrap module to programmatically wrap, fill, indent, dedent, and shorten text.

14 / 15
Python
import textwrap text = "The quick brown fox jumps over the lazy dog. " * 3 # Wrap into a list of lines (width in characters) lines = textwrap.wrap(text, width=40) # Wrap and rejoin into a single string with newlines wrapped = textwrap.fill(text, width=40) print(wrapped)
Recommendation
Add an example combining dedent+fill together as mentioned in best practices to reinforce the pattern
14 / 15

Progress:

  • Identify the goal: list of lines (wrap), single string (fill), truncation (shorten), whitespace normalization (dedent), or adding prefixes (indent)
  • Choose the right function for that goal
  • Configure relevant keyword arguments (width, prefixes, break behavior)
  • Apply to input text
  • Verify output formatting (line lengths, indentation, no unwanted whitespace)

Function selection guide

NeedFunction
List of wrapped linestextwrap.wrap(text, width=70)
Single wrapped stringtextwrap.fill(text, width=70)
Truncate to fit width + add [...]textwrap.shorten(text, width=50)
Remove common leading whitespacetextwrap.dedent(text)
Add prefix to each linetextwrap.indent(text, prefix)
Reusable wrapper with custom settingstextwrap.TextWrapper(**options)

Key options (for wrap/fill/TextWrapper)

  • width (default 70) — max line length
  • initial_indent / subsequent_indent — string prepended to first/later lines
  • break_long_words (default True) — split words longer than width
  • break_on_hyphens (default True) — allow breaking at hyphens
  • replace_whitespace (default True) — collapse all whitespace to single spaces
  • drop_whitespace (default True) — strip leading/trailing whitespace per line
  • max_lines — truncate output, adding a placeholder (default ' [...]') on the last line
  • tabsize — expand tabs to this many spaces before processing (default 8)
Recommendation
Include a brief note on performance/limits with very large texts
19 / 20

Example 1: Wrapping paragraph for terminal output Input:

Python
textwrap.fill("This is a very long line of text that needs wrapping for a narrow terminal.", width=30)

Output:

This is a very long line of
text that needs wrapping for
a narrow terminal.

Example 2: Dedenting a triple-quoted string Input:

Python
text = """ def foo(): return 1 """ print(textwrap.dedent(text))

Output:


def foo():
    return 1

Example 3: Indenting with a prefix (e.g., for quoting/logging) Input:

Python
textwrap.indent("line one\nline two", "> ")

Output:

> line one
> line two

Example 4: Shortening text with placeholder Input:

Python
textwrap.shorten("The quick brown fox jumps over the lazy dog", width=25)

Output:

The quick brown fox [...]

Example 5: Reusable wrapper for hanging indent Input:

Python
wrapper = textwrap.TextWrapper(width=40, initial_indent="- ", subsequent_indent=" ") print(wrapper.fill("This is a bullet point that wraps onto multiple lines nicely."))

Output:

- This is a bullet point that wraps
  onto multiple lines nicely.
Recommendation
Consider mentioning textwrap vs alternatives (e.g., third-party libs) for non-monospace or Unicode-aware wrapping edge cases
  • Use dedent() before fill()/wrap() when processing triple-quoted strings defined inside indented code — combine as textwrap.fill(textwrap.dedent(text)).
  • Prefer TextWrapper over repeated wrap()/fill() calls when applying the same settings many times — it avoids re-parsing options.
  • Use initial_indent/subsequent_indent for bullet points, blockquotes, or comment-style prefixes instead of manual string concatenation.
  • Set break_long_words=False and break_on_hyphens=False when wrapping text containing URLs, file paths, or code identifiers that shouldn't be split.
  • Use shorten() for generating previews/summaries (e.g., truncated log lines, table cells) since it collapses whitespace and adds a clean placeholder automatically.
  • Don't use dedent() on text with mixed tabs/spaces without normalizing first — it only strips leading whitespace common to all lines, so inconsistent indentation results in no-op.
  • Don't forget dedent() operates on the whole text as a block; it won't help per-line indentation removal for irregularly indented lines.
  • Don't assume wrap() preserves original line breaks — replace_whitespace=True (the default) collapses newlines/tabs into spaces before wrapping.
  • Don't use fill() when you need the individual lines for further processing (e.g., adding line numbers) — use wrap() and join manually instead.
  • Remember indent() by default adds the prefix to lines consisting solely of whitespace too; pass a predicate function to control which lines get prefixed.
0
Grade AAI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
14/15
Workflow
14/15
Examples
19/20
Completeness
19/20
Format
15/15
Conciseness
14/15