Skip to main content

textwrap

Word-wrap, dedent, indent, and shorten human-readable text.

Source-of-record: Lib/textwrap.py, textwrap docs.

Functions

FunctionEffect
wrap(text, width=70, **kw)List of wrapped lines.
fill(text, width=70, **kw)Single string with \\n joins.
shorten(text, width, *, placeholder=' [...]')Truncate at word boundary.
dedent(text)Remove common leading whitespace.
indent(text, prefix, predicate=None)Prepend prefix to lines.

TextWrapper parameters

NameDefaultEffect
width70Target width.
initial_indent''First-line prefix.
subsequent_indent''Subsequent-line prefix.
expand_tabsTrueTabs to spaces.
tabsize8Tab width.
replace_whitespaceTrueWhitespace -> single space.
fix_sentence_endingsFalseTwo spaces between sentences.
break_long_wordsTrueBreak words longer than width.
break_on_hyphensTrueAllow breaking on hyphens.
drop_whitespaceTrueStrip whitespace from line ends.
max_linesNoneLimit; uses placeholder.
placeholder' [...]'Suffix when truncated.

Gopy status

AreaState
All free functionsComplete.
TextWrapper and its optionsComplete.

Reference

  • CPython 3.14: textwrap.
  • Lib/textwrap.py.
  • module/textwrap/. gopy port.