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
widthis the maximum line length. As long as no single word is longer thanwidth, no output line will be longer.break_long_words=Truesplits words longer thanwidth;break_on_hyphens=Truelets 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 optionalpredicateto 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
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.
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
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.},
}
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.
- python-textwrap.table.json (table; CC-BY-4.0)
- python-textwrap.schema.json (schema; CC-BY-4.0)
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.