blob: 50c710c522e4e840e467405f986b0edae6c8edb6 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`textwrap` --- Text wrapping and filling
2=============================================
3
4.. module:: textwrap
5 :synopsis: Text wrapping and filling
6.. moduleauthor:: Greg Ward <gward@python.net>
7.. sectionauthor:: Greg Ward <gward@python.net>
8
Raymond Hettinger10480942011-01-10 03:26:08 +00009**Source code:** :source:`Lib/textwrap.py`
Georg Brandl116aa622007-08-15 14:28:22 +000010
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000011--------------
12
Georg Brandl116aa622007-08-15 14:28:22 +000013The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and
14:func:`fill`, as well as :class:`TextWrapper`, the class that does all the work,
Nick Coghlan4fae8cd2012-06-11 23:07:51 +100015and two utility functions, :func:`dedent` and :func:`indent`. If you're just wrapping or filling one
Georg Brandl116aa622007-08-15 14:28:22 +000016or two text strings, the convenience functions should be good enough;
17otherwise, you should use an instance of :class:`TextWrapper` for efficiency.
18
Georg Brandl7f01a132009-09-16 15:58:14 +000019.. function:: wrap(text, width=70, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000020
Georg Brandl7f01a132009-09-16 15:58:14 +000021 Wraps the single paragraph in *text* (a string) so every line is at most
22 *width* characters long. Returns a list of output lines, without final
23 newlines.
Georg Brandl116aa622007-08-15 14:28:22 +000024
25 Optional keyword arguments correspond to the instance attributes of
26 :class:`TextWrapper`, documented below. *width* defaults to ``70``.
27
28
Georg Brandl7f01a132009-09-16 15:58:14 +000029.. function:: fill(text, width=70, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000030
31 Wraps the single paragraph in *text*, and returns a single string containing the
32 wrapped paragraph. :func:`fill` is shorthand for ::
33
34 "\n".join(wrap(text, ...))
35
36 In particular, :func:`fill` accepts exactly the same keyword arguments as
37 :func:`wrap`.
38
39Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper`
40instance and calling a single method on it. That instance is not reused, so for
41applications that wrap/fill many text strings, it will be more efficient for you
42to create your own :class:`TextWrapper` object.
43
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000044Text is preferably wrapped on whitespaces and right after the hyphens in
45hyphenated words; only then will long words be broken if necessary, unless
46:attr:`TextWrapper.break_long_words` is set to false.
47
Nick Coghlan4fae8cd2012-06-11 23:07:51 +100048Two additional utility function, :func:`dedent` and :func:`indent`, are
49provided to remove indentation from strings that have unwanted whitespace
50to the left of the text and to add an arbitrary prefix to selected lines
51in a block of text.
Georg Brandl116aa622007-08-15 14:28:22 +000052
53.. function:: dedent(text)
54
55 Remove any common leading whitespace from every line in *text*.
56
57 This can be used to make triple-quoted strings line up with the left edge of the
58 display, while still presenting them in the source code in indented form.
59
60 Note that tabs and spaces are both treated as whitespace, but they are not
61 equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
Georg Brandle6bcc912008-05-12 18:05:20 +000062 common leading whitespace.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64 For example::
65
66 def test():
67 # end first line with \ to avoid the empty line!
68 s = '''\
69 hello
70 world
71 '''
Collin Winterc79461b2007-09-01 23:34:30 +000072 print(repr(s)) # prints ' hello\n world\n '
73 print(repr(dedent(s))) # prints 'hello\n world\n'
Georg Brandl116aa622007-08-15 14:28:22 +000074
75
Nick Coghlan4fae8cd2012-06-11 23:07:51 +100076.. function:: indent(text, prefix, predicate=None)
77
78 Add *prefix* to the beginning of selected lines in *text*.
79
80 Lines are separated by calling ``text.splitlines(True)``.
81
82 By default, *prefix* is added to all lines that do not consist
83 solely of whitespace (including any line endings).
84
85 For example::
86
87 >>> s = 'hello\n\n \nworld'
88 >>> indent(s, ' ')
89 ' hello\n\n \n world'
90
91 The optional *predicate* argument can be used to control which lines
92 are indented. For example, it is easy to add *prefix* to even empty
93 and whitespace-only lines::
94
95 >>> print(indent(s, '+ ', lambda line: True))
96 + hello
97 +
98 +
99 + world
100
101
Georg Brandl7f01a132009-09-16 15:58:14 +0000102.. class:: TextWrapper(**kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104 The :class:`TextWrapper` constructor accepts a number of optional keyword
Georg Brandl7f01a132009-09-16 15:58:14 +0000105 arguments. Each keyword argument corresponds to an instance attribute, so
106 for example ::
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108 wrapper = TextWrapper(initial_indent="* ")
109
110 is the same as ::
111
112 wrapper = TextWrapper()
113 wrapper.initial_indent = "* "
114
115 You can re-use the same :class:`TextWrapper` object many times, and you can
116 change any of its options through direct assignment to instance attributes
117 between uses.
118
Benjamin Petersone41251e2008-04-25 01:59:09 +0000119 The :class:`TextWrapper` instance attributes (and keyword arguments to the
120 constructor) are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 .. attribute:: width
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 (default: ``70``) The maximum length of wrapped lines. As long as there
126 are no individual words in the input text longer than :attr:`width`,
127 :class:`TextWrapper` guarantees that no output line will be longer than
128 :attr:`width` characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 .. attribute:: expand_tabs
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Benjamin Petersone41251e2008-04-25 01:59:09 +0000133 (default: ``True``) If true, then all tab characters in *text* will be
134 expanded to spaces using the :meth:`expandtabs` method of *text*.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
Hynek Schlawackd5272592012-05-19 13:33:11 +0200137 .. attribute:: tabsize
138
139 (default: ``8``) If :attr:`expand_tabs` is true, then all tab characters
140 in *text* will be expanded to zero or more spaces, depending on the
141 current column and the given tab size.
142
143 .. versionadded:: 3.3
144
145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 .. attribute:: replace_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Benjamin Petersone41251e2008-04-25 01:59:09 +0000148 (default: ``True``) If true, each whitespace character (as defined by
149 ``string.whitespace``) remaining after tab expansion will be replaced by a
150 single space.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Benjamin Petersone41251e2008-04-25 01:59:09 +0000152 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Benjamin Petersone41251e2008-04-25 01:59:09 +0000154 If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true,
155 each tab character will be replaced by a single space, which is *not*
156 the same as tab expansion.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Terry Reedy6d2ab712010-11-23 20:17:24 +0000158 .. note::
159
160 If :attr:`replace_whitespace` is false, newlines may appear in the
161 middle of a line and cause strange output. For this reason, text should
162 be split into paragraphs (using :meth:`str.splitlines` or similar)
163 which are wrapped separately.
164
Georg Brandl116aa622007-08-15 14:28:22 +0000165
Benjamin Petersone41251e2008-04-25 01:59:09 +0000166 .. attribute:: drop_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Benjamin Petersone41251e2008-04-25 01:59:09 +0000168 (default: ``True``) If true, whitespace that, after wrapping, happens to
169 end up at the beginning or end of a line is dropped (leading whitespace in
170 the first line is always preserved, though).
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Benjamin Petersone41251e2008-04-25 01:59:09 +0000173 .. attribute:: initial_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Benjamin Petersone41251e2008-04-25 01:59:09 +0000175 (default: ``''``) String that will be prepended to the first line of
176 wrapped output. Counts towards the length of the first line.
Georg Brandl116aa622007-08-15 14:28:22 +0000177
178
Benjamin Petersone41251e2008-04-25 01:59:09 +0000179 .. attribute:: subsequent_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000180
Benjamin Petersone41251e2008-04-25 01:59:09 +0000181 (default: ``''``) String that will be prepended to all lines of wrapped
182 output except the first. Counts towards the length of each line except
183 the first.
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185
Benjamin Petersone41251e2008-04-25 01:59:09 +0000186 .. attribute:: fix_sentence_endings
Georg Brandl116aa622007-08-15 14:28:22 +0000187
Benjamin Petersone41251e2008-04-25 01:59:09 +0000188 (default: ``False``) If true, :class:`TextWrapper` attempts to detect
189 sentence endings and ensure that sentences are always separated by exactly
190 two spaces. This is generally desired for text in a monospaced font.
191 However, the sentence detection algorithm is imperfect: it assumes that a
192 sentence ending consists of a lowercase letter followed by one of ``'.'``,
193 ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
194 followed by a space. One problem with this is algorithm is that it is
195 unable to detect the difference between "Dr." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 [...] Dr. Frankenstein's monster [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000198
Benjamin Petersone41251e2008-04-25 01:59:09 +0000199 and "Spot." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000200
Benjamin Petersone41251e2008-04-25 01:59:09 +0000201 [...] See Spot. See Spot run [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000202
Benjamin Petersone41251e2008-04-25 01:59:09 +0000203 :attr:`fix_sentence_endings` is false by default.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
Benjamin Petersone41251e2008-04-25 01:59:09 +0000205 Since the sentence detection algorithm relies on ``string.lowercase`` for
206 the definition of "lowercase letter," and a convention of using two spaces
207 after a period to separate sentences on the same line, it is specific to
208 English-language texts.
Georg Brandl116aa622007-08-15 14:28:22 +0000209
210
Benjamin Petersone41251e2008-04-25 01:59:09 +0000211 .. attribute:: break_long_words
Georg Brandl116aa622007-08-15 14:28:22 +0000212
Benjamin Petersone41251e2008-04-25 01:59:09 +0000213 (default: ``True``) If true, then words longer than :attr:`width` will be
214 broken in order to ensure that no lines are longer than :attr:`width`. If
215 it is false, long words will not be broken, and some lines may be longer
216 than :attr:`width`. (Long words will be put on a line by themselves, in
217 order to minimize the amount by which :attr:`width` is exceeded.)
Georg Brandl116aa622007-08-15 14:28:22 +0000218
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000219
220 .. attribute:: break_on_hyphens
221
222 (default: ``True``) If true, wrapping will occur preferably on whitespaces
223 and right after hyphens in compound words, as it is customary in English.
224 If false, only whitespaces will be considered as potentially good places
225 for line breaks, but you need to set :attr:`break_long_words` to false if
226 you want truly insecable words. Default behaviour in previous versions
227 was to always allow breaking hyphenated words.
228
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000229
Benjamin Petersone41251e2008-04-25 01:59:09 +0000230 :class:`TextWrapper` also provides two public methods, analogous to the
231 module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Benjamin Petersone41251e2008-04-25 01:59:09 +0000233 .. method:: wrap(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Benjamin Petersone41251e2008-04-25 01:59:09 +0000235 Wraps the single paragraph in *text* (a string) so every line is at most
236 :attr:`width` characters long. All wrapping options are taken from
237 instance attributes of the :class:`TextWrapper` instance. Returns a list
238 of output lines, without final newlines.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
240
Benjamin Petersone41251e2008-04-25 01:59:09 +0000241 .. method:: fill(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000242
Benjamin Petersone41251e2008-04-25 01:59:09 +0000243 Wraps the single paragraph in *text*, and returns a single string
244 containing the wrapped paragraph.
Georg Brandl116aa622007-08-15 14:28:22 +0000245