On this page
textwrap.TextWrapper
Prev: textwrap.shorten | Up: textwrap | Next: textwrap.wrap
Rel: textwrap.dedent, textwrap.fill, textwrap.indent, textwrap.shorten
Ref: docs.python.org โ textwrap.TextWrapper
Tags: #public #python3 #stdlib #textwrap
Import
import textwrap
from textwrap import TextWrapper
Signature & Overview
textwrap.TextWrapper(width=70, initial_indent='', subsequent_indent='', expand_tabs=True, replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, drop_whitespace=True, break_on_hyphens=True, tabsize=8, *, max_lines=None, placeholder=' [...]')
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 & Attributes
TextWrapper.fill(self, text)โ Reformat the single paragraph in 'text' to fit in lines of no more than 'self.width' columns, and return a new string containing the entire wrapped paragraph.TextWrapper.wrap(self, text)โ Reformat the single paragraph in 'text' so it fits in lines of no more than 'self.width' columns, and return a list of wrapped lines. Tabs in 'text' are expanded with string.expandtabs(),
Usage
import textwrap
w = textwrap.TextWrapper(width=20, subsequent_indent=' ')
assert w.fill('one two three four five six') == 'one two three four\n five six'
Gotchas
- Reuse a
TextWrapperinstance in loops instead of callingtextwrap.wrap()repeatedly.
REPL Inspect
from textwrap import TextWrapper
help(TextWrapper)
Linked from
Related notes
- tuts/python3/stdlib_module/textwrap/dedent Linked here; links to this page; shared tags: #stdlib, #textwrap
- tuts/python3/stdlib_module/textwrap/fill Linked here; links to this page; shared tags: #stdlib, #textwrap
- tuts/python3/stdlib_module/textwrap/indent Linked here; links to this page; shared tags: #stdlib, #textwrap
- tuts/python3/stdlib_module/textwrap/shorten Linked here; links to this page; shared tags: #stdlib, #textwrap
- tuts/python3/stdlib_module/textwrap/wrap Linked here; links to this page; shared tags: #stdlib, #textwrap
- tuts/python3/stdlib_module/textwrap Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib