all Python 3textwrapterm
textwrap
module
Wrap and fill paragraphs to a width, shorten long lines, remove and add indentation.
Syntax
import textwrap
textwrap lays plain text out to a width: wrap returns a list of lines, fill returns one string with newlines, shorten truncates at a word boundary with a placeholder. dedent strips the common indent from a triple-quoted string, indent adds a prefix. Width is counted in characters, so emoji and CJK characters that take two terminal cells break the alignment.
Examples
import textwrap
s = 'The quick brown fox jumps over the lazy dog'
print(textwrap.wrap(s, width=15)) # → ['The quick brown', 'fox jumps over', 'the lazy dog']
print(textwrap.shorten(s, width=20)) # → The quick [...]
print(textwrap.dedent(' a\n b')) # → a / b
See also
Open in the full glossarydocs.python.orgReport an inaccuracy