---
source: https://iinventedlight.com/library/notes/python-textwrap.md
title: "Python textwrap - wrap, fill, shorten, dedent and indent plain text"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, textwrap - Text wrapping and filling; python/cpython (GitHub), Lib/textwrap.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: textwrap"
tags: [python, textwrap, strings, formatting, cli, stdlib]
client: Librarian
sources:
  - id: S1
    title: "textwrap - Text wrapping and filling"
    publisher: Python docs
    url: https://docs.python.org/3/library/textwrap.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/textwrap.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/textwrap.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python textwrap - wrap, fill, shorten, dedent and indent plain text
**Answer:** `textwrap.fill(text, width=70)` wraps the single paragraph in text and returns a single string containing the wrapped paragraph. `wrap()` returns a list of output lines instead.

## Examples

```python
import textwrap

text = "The quick brown fox jumps over the lazy dog. " * 3
print(textwrap.fill(text, width=40))
lines = textwrap.wrap(text, width=40, initial_indent="* ", subsequent_indent="  ")

textwrap.shorten("Hello  world!", width=12)          # 'Hello world!'
textwrap.shorten("Hello world!", width=11)           # 'Hello [...]'

usage = textwrap.dedent("""\
    usage: tool [options]
      -v  verbose
""")
print(textwrap.indent(usage, "> "))

wrapper = textwrap.TextWrapper(width=50, max_lines=3, placeholder=" ...")
print(wrapper.fill(text))
```

## Functions

| Function | Returns |
|---|---|
| `wrap(text, width=70)` | a list of output lines, without final newlines |
| `fill(text, width=70)` | a single string; shorthand for `"\n".join(wrap(text))` |
| `shorten(text, width)` | collapsed whitespace, then words dropped from the end plus the placeholder `' [...]'` |
| `dedent(text)` | text with common leading whitespace removed from every line |
| `indent(text, prefix)` | text with prefix added to lines that are not only whitespace |

## Details

- `width` is the maximum line length. As long as no single word is longer than `width`, no output line will be longer.
- `break_long_words=True` splits words longer than `width`; `break_on_hyphens=True` lets lines break after hyphens.
- `dedent()` lets triple-quoted strings line up with the left edge while staying indented in source. Tabs and spaces are both treated as whitespace, but they are not equal.
- `indent()` takes an optional `predicate` to choose which lines get the prefix.

## TextWrapper

For repeated use, create a `TextWrapper`; each keyword argument corresponds to an instance attribute, and you can reuse the same object many times.

## See also

- [python-f-strings](https://iinventedlight.com/library/notes/python-f-strings/)
- [python-argparse](https://iinventedlight.com/library/notes/python-argparse/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## Claims

- id: C1
  claim: wrap returns lines without final newlines.
  source: S1
  evidence: "Returns a list of output lines, without final newlines."

- id: C2
  claim: fill is shorthand for joining wrap output.
  source: S1
  evidence: "fill() is shorthand for"

- id: C3
  claim: dedent removes common leading whitespace.
  source: S2
  evidence: "Remove any common leading whitespace from every line in `text`."

## Sources

- [S1] textwrap - Text wrapping and filling. Python docs. https://docs.python.org/3/library/textwrap.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/textwrap.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/textwrap.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, textwrap - Text wrapping and filling; python/cpython (GitHub), Lib/textwrap.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
