Navigation

The textwrap module provides some convenience functions,
as well as TextWrapper, the class that does all the work.
If you’re just wrapping or filling one or two text strings, the convenience
functions should be good enough; otherwise, you should use an instance of
TextWrapper for efficiency.

First the whitespace in text is collapsed (all whitespace is replaced by
single spaces). If the result fits in the width, it is returned.
Otherwise, enough words are dropped from the end so that the remaining words
plus the placeholder fit within width:

By default, prefix is added to all lines that do not consist
solely of whitespace (including any line endings).

For example:

>>> s='hello\n\n\nworld'>>> indent(s,' ')' hello\n\n \n world'

The optional predicate argument can be used to control which lines
are indented. For example, it is easy to add prefix to even empty
and whitespace-only lines:

>>> print(indent(s,'+ ',lambdaline:True))+ hello+++ world

New in version 3.3.

wrap(), fill() and shorten() work by creating a
TextWrapper instance and calling a single method on it. That
instance is not reused, so for applications that process many text
strings using wrap() and/or fill(), it may be more efficient to
create your own TextWrapper object.

Text is preferably wrapped on whitespaces and right after the hyphens in
hyphenated words; only then will long words be broken if necessary, unless
TextWrapper.break_long_words is set to false.

(default: 70) The maximum length of wrapped lines. As long as there
are no individual words in the input text longer than width,
TextWrapper guarantees that no output line will be longer than
width characters.

(default: True) If true, after tab expansion but before wrapping,
the wrap() method will replace each whitespace character
with a single space. The whitespace characters replaced are
as follows: tab, newline, vertical tab, formfeed, and carriage
return ('\t\n\v\f\r').

Note

If expand_tabs is false and replace_whitespace is true,
each tab character will be replaced by a single space, which is not
the same as tab expansion.

Note

If replace_whitespace is false, newlines may appear in the
middle of a line and cause strange output. For this reason, text should
be split into paragraphs (using str.splitlines() or similar)
which are wrapped separately.

(default: True) If true, whitespace at the beginning and ending of
every line (after wrapping but before indenting) is dropped.
Whitespace at the beginning of the paragraph, however, is not dropped
if non-whitespace follows it. If whitespace being dropped takes up an
entire line, the whole line is dropped.

(default: False) If true, TextWrapper attempts to detect
sentence endings and ensure that sentences are always separated by exactly
two spaces. This is generally desired for text in a monospaced font.
However, the sentence detection algorithm is imperfect: it assumes that a
sentence ending consists of a lowercase letter followed by one of '.',
'!', or '?', possibly followed by one of '"' or "'",
followed by a space. One problem with this is algorithm is that it is
unable to detect the difference between “Dr.” in

Since the sentence detection algorithm relies on string.lowercase for
the definition of “lowercase letter,” and a convention of using two spaces
after a period to separate sentences on the same line, it is specific to
English-language texts.

(default: True) If true, then words longer than width will be
broken in order to ensure that no lines are longer than width. If
it is false, long words will not be broken, and some lines may be longer
than width. (Long words will be put on a line by themselves, in
order to minimize the amount by which width is exceeded.)

(default: True) If true, wrapping will occur preferably on whitespaces
and right after hyphens in compound words, as it is customary in English.
If false, only whitespaces will be considered as potentially good places
for line breaks, but you need to set break_long_words to false if
you want truly insecable words. Default behaviour in previous versions
was to always allow breaking hyphenated words.

Wraps the single paragraph in text (a string) so every line is at most
width characters long. All wrapping options are taken from
instance attributes of the TextWrapper instance. Returns a list
of output lines, without final newlines. If the wrapped output has no
content, the returned list is empty.