On this page
textwrap
Prev: termios | Up: stdlib_module | Next: this
Rel: string, re, difflib, unicodedata, stringprep
Ref: docs.python.org — textwrap
Tags: #public #python3 #stdlib
Import
import textwrap
Overview
Paragraph wrapping, indentation, dedentation, and truncation utilities (dedent, indent, fill, wrap, shorten). Reach for textwrap.dedent() for clean multi-line triple-quoted strings inside indented functions and tests, indent() for prefixing blocks, and shorten() for word-boundary summaries.
Quick Start
import textwrap
block = textwrap.dedent('''
line one
indented line two
''').lstrip("\n")
assert block == "line one\n indented line two\n"
assert textwrap.shorten("Python standard library utilities", width=20, placeholder="...") == "Python standard..."
Implementation Walkthrough
1. Wrapping & Prefixing Multi-Line CLI Output (fill + indent)
import textwrap
body = "A long diagnostic message explaining why a configuration file failed validation."
wrapped = textwrap.fill(body, width=40)
quoted = textwrap.indent(wrapped, prefix=" > ")
assert all(line.startswith(" > ") for line in quoted.splitlines())
2. Custom Line Filtering in indent()
Pass a predicate= callable to indent even blank lines (by default, indent() skips whitespace-only lines):
import textwrap
assert textwrap.indent("a\n\nb", "> ", predicate=lambda _: True) == "> a\n> \n> b"
Gotchas & Boundaries
dedent()only strips common leading whitespace shared by all non-empty lines: Put a\after the opening triple quote (or.lstrip("\n")) so the first line doesn't leave a stray blank line.shorten()collapses all internal whitespace (including newlines) into single spaces before truncating at word boundaries.- Unicode wide characters (CJK/emoji):
textwrapcounts Python code points, not terminal display column widths.
Common Functions
- textwrap.dedent() —
dedent(text)— Remove any common leading whitespace from every line intext. - textwrap.fill() —
fill(text, width=70, **kwargs)— Fill a single paragraph of text, returning a new string. - textwrap.indent() —
indent(text, prefix, predicate=None)— Adds 'prefix' to the beginning of selected lines in 'text'. - textwrap.shorten() —
shorten(text, width, **kwargs)— Collapse and truncate the given text to fit in the given width. - textwrap.wrap() —
wrap(text, width=70, **kwargs)— Wrap a single paragraph of text, returning a list of wrapped lines.
Classes & Objects
- textwrap.TextWrapper — Object for wrapping/filling text. The public interface consists of the wrap() and fill() methods; the other methods are just there for subclasses to override in order to tweak the default behaviour. (Methods:
.fill(),.wrap())
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/difflib
- tuts/python3/stdlib_module/re
- tuts/python3/stdlib_module/readline
- tuts/python3/stdlib_module/rlcompleter
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/string
- tuts/python3/stdlib_module/stringprep
- tuts/python3/stdlib_module/termios
- tuts/python3/stdlib_module/textwrap/dedent
- tuts/python3/stdlib_module/textwrap/fill
- tuts/python3/stdlib_module/textwrap/indent
- tuts/python3/stdlib_module/textwrap/shorten
- tuts/python3/stdlib_module/textwrap/TextWrapper
- tuts/python3/stdlib_module/textwrap/wrap
- tuts/python3/stdlib_module/this
- tuts/python3/stdlib_module/unicodedata
Related notes
- tuts/python3/stdlib_module/difflib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/re Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/string Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stringprep Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/textwrap/dedent Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/textwrap/fill Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/textwrap/indent Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/textwrap/shorten Linked here; links to this page; shared tags: #stdlib