2
:mod:`textwrap` --- Text wrapping and filling
3
=============================================
6
:synopsis: Text wrapping and filling
7
.. moduleauthor:: Greg Ward <gward@python.net>
8
.. sectionauthor:: Greg Ward <gward@python.net>
11
The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and
12
:func:`fill`, as well as :class:`TextWrapper`, the class that does all the work,
13
and a utility function :func:`dedent`. If you're just wrapping or filling one
14
or two text strings, the convenience functions should be good enough;
15
otherwise, you should use an instance of :class:`TextWrapper` for efficiency.
18
.. function:: wrap(text[, width[, ...]])
20
Wraps the single paragraph in *text* (a string) so every line is at most *width*
21
characters long. Returns a list of output lines, without final newlines.
23
Optional keyword arguments correspond to the instance attributes of
24
:class:`TextWrapper`, documented below. *width* defaults to ``70``.
27
.. function:: fill(text[, width[, ...]])
29
Wraps the single paragraph in *text*, and returns a single string containing the
30
wrapped paragraph. :func:`fill` is shorthand for ::
32
"\n".join(wrap(text, ...))
34
In particular, :func:`fill` accepts exactly the same keyword arguments as
37
Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper`
38
instance and calling a single method on it. That instance is not reused, so for
39
applications that wrap/fill many text strings, it will be more efficient for you
40
to create your own :class:`TextWrapper` object.
42
Text is preferably wrapped on whitespaces and right after the hyphens in
43
hyphenated words; only then will long words be broken if necessary, unless
44
:attr:`TextWrapper.break_long_words` is set to false.
46
An additional utility function, :func:`dedent`, is provided to remove
47
indentation from strings that have unwanted whitespace to the left of the text.
50
.. function:: dedent(text)
52
Remove any common leading whitespace from every line in *text*.
54
This can be used to make triple-quoted strings line up with the left edge of the
55
display, while still presenting them in the source code in indented form.
57
Note that tabs and spaces are both treated as whitespace, but they are not
58
equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
59
common leading whitespace.
64
# end first line with \ to avoid the empty line!
69
print(repr(s)) # prints ' hello\n world\n '
70
print(repr(dedent(s))) # prints 'hello\n world\n'
73
.. class:: TextWrapper(...)
75
The :class:`TextWrapper` constructor accepts a number of optional keyword
76
arguments. Each argument corresponds to one instance attribute, so for example
79
wrapper = TextWrapper(initial_indent="* ")
83
wrapper = TextWrapper()
84
wrapper.initial_indent = "* "
86
You can re-use the same :class:`TextWrapper` object many times, and you can
87
change any of its options through direct assignment to instance attributes
90
The :class:`TextWrapper` instance attributes (and keyword arguments to the
91
constructor) are as follows:
96
(default: ``70``) The maximum length of wrapped lines. As long as there
97
are no individual words in the input text longer than :attr:`width`,
98
:class:`TextWrapper` guarantees that no output line will be longer than
99
:attr:`width` characters.
102
.. attribute:: expand_tabs
104
(default: ``True``) If true, then all tab characters in *text* will be
105
expanded to spaces using the :meth:`expandtabs` method of *text*.
108
.. attribute:: replace_whitespace
110
(default: ``True``) If true, each whitespace character (as defined by
111
``string.whitespace``) remaining after tab expansion will be replaced by a
116
If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true,
117
each tab character will be replaced by a single space, which is *not*
118
the same as tab expansion.
121
.. attribute:: drop_whitespace
123
(default: ``True``) If true, whitespace that, after wrapping, happens to
124
end up at the beginning or end of a line is dropped (leading whitespace in
125
the first line is always preserved, though).
128
.. attribute:: initial_indent
130
(default: ``''``) String that will be prepended to the first line of
131
wrapped output. Counts towards the length of the first line.
134
.. attribute:: subsequent_indent
136
(default: ``''``) String that will be prepended to all lines of wrapped
137
output except the first. Counts towards the length of each line except
141
.. attribute:: fix_sentence_endings
143
(default: ``False``) If true, :class:`TextWrapper` attempts to detect
144
sentence endings and ensure that sentences are always separated by exactly
145
two spaces. This is generally desired for text in a monospaced font.
146
However, the sentence detection algorithm is imperfect: it assumes that a
147
sentence ending consists of a lowercase letter followed by one of ``'.'``,
148
``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
149
followed by a space. One problem with this is algorithm is that it is
150
unable to detect the difference between "Dr." in ::
152
[...] Dr. Frankenstein's monster [...]
156
[...] See Spot. See Spot run [...]
158
:attr:`fix_sentence_endings` is false by default.
160
Since the sentence detection algorithm relies on ``string.lowercase`` for
161
the definition of "lowercase letter," and a convention of using two spaces
162
after a period to separate sentences on the same line, it is specific to
163
English-language texts.
166
.. attribute:: break_long_words
168
(default: ``True``) If true, then words longer than :attr:`width` will be
169
broken in order to ensure that no lines are longer than :attr:`width`. If
170
it is false, long words will not be broken, and some lines may be longer
171
than :attr:`width`. (Long words will be put on a line by themselves, in
172
order to minimize the amount by which :attr:`width` is exceeded.)
175
.. attribute:: break_on_hyphens
177
(default: ``True``) If true, wrapping will occur preferably on whitespaces
178
and right after hyphens in compound words, as it is customary in English.
179
If false, only whitespaces will be considered as potentially good places
180
for line breaks, but you need to set :attr:`break_long_words` to false if
181
you want truly insecable words. Default behaviour in previous versions
182
was to always allow breaking hyphenated words.
185
:class:`TextWrapper` also provides two public methods, analogous to the
186
module-level convenience functions:
188
.. method:: wrap(text)
190
Wraps the single paragraph in *text* (a string) so every line is at most
191
:attr:`width` characters long. All wrapping options are taken from
192
instance attributes of the :class:`TextWrapper` instance. Returns a list
193
of output lines, without final newlines.
196
.. method:: fill(text)
198
Wraps the single paragraph in *text*, and returns a single string
199
containing the wrapped paragraph.