← All notes

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

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

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:0z6n2ync

iinventedlight. 2026. "Python textwrap - wrap, fill, shorten, dedent and indent plain text." iinventedlight Library, iil:note:0z6n2ync, version of 2026-11-18. https://iinventedlight.com/library/notes/python-textwrap/. License: CC-BY-4.0.

@misc{iil_note_0z6n2ync,
  author = {{iinventedlight}},
  title = {{Python textwrap - wrap, fill, shorten, dedent and indent plain text}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/python-textwrap/},
  note = {Permanent ID iil:note:0z6n2ync. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-textwrap.md.