all Python 3textwrapterm
textwrap.TextWrapper
wrapping
A reusable text-wrapping object configured with width, indents, etc.
Syntax
textwrap.TextWrapper(width=70, **kwargs)
wrap() and fill() are thin wrappers that build a throwaway TextWrapper on every call, so when you format many chunks with the same settings, keeping one instance around is cheaper. Its options are plain attributes you can change between calls — handy when width has to follow the current terminal size.
Examples
import textwrap
w = textwrap.TextWrapper(width=10)
print(w.wrap('a b c d')) # → ['a b c d']
print(w.wrap('one two three four')) # → ['one two', 'three four']
print(textwrap.TextWrapper(width=12, initial_indent='> ').wrap('one two three')) # → ['> one two', 'three']
print(textwrap.TextWrapper(width=5).wrap('abcdefgh')) # → ['abcde', 'fgh']
print(w.wrap('')) # → []
See also
Open in the full glossarydocs.python.orgReport an inaccuracy