blob: 6ac1e77497da637818d0bb0df34f1c73652426ec [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
Antoine Pitrou389dec82013-08-12 22:39:09 +020013The :mod:`textwrap` module provides some convenience functions,
14as well as :class:`TextWrapper`, the class that does all the work.
15If you're just wrapping or filling one or two text strings, the convenience
16functions should be good enough; otherwise, you should use an instance of
17:class:`TextWrapper` for efficiency.
Georg Brandl116aa622007-08-15 14:28:22 +000018
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
R David Murray1585b702012-09-08 13:13:25 -040028 See the :meth:`TextWrapper.wrap` method for additional details on how
29 :func:`wrap` behaves.
30
Georg Brandl116aa622007-08-15 14:28:22 +000031
Georg Brandl7f01a132009-09-16 15:58:14 +000032.. function:: fill(text, width=70, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000033
34 Wraps the single paragraph in *text*, and returns a single string containing the
35 wrapped paragraph. :func:`fill` is shorthand for ::
36
37 "\n".join(wrap(text, ...))
38
39 In particular, :func:`fill` accepts exactly the same keyword arguments as
40 :func:`wrap`.
41
Georg Brandl116aa622007-08-15 14:28:22 +000042
Antoine Pitrouc5930562013-08-16 22:31:12 +020043.. function:: shorten(text, width=70, *, placeholder=" [...]")
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000044
Antoine Pitrou389dec82013-08-12 22:39:09 +020045 Collapse and truncate the given text to fit in the given width.
46
47 The text first has its whitespace collapsed. If it then fits in
48 the *width*, it is returned unchanged. Otherwise, as many words
49 as possible are joined and then the *placeholder* is appended::
50
51 >>> textwrap.shorten("Hello world!", width=12)
52 'Hello world!'
53 >>> textwrap.shorten("Hello world!", width=11)
Antoine Pitrouc5930562013-08-16 22:31:12 +020054 'Hello [...]'
Antoine Pitrou389dec82013-08-12 22:39:09 +020055 >>> textwrap.shorten("Hello world", width=10, placeholder="...")
56 'Hello...'
57
58 .. versionadded:: 3.4
59
Georg Brandl116aa622007-08-15 14:28:22 +000060
61.. function:: dedent(text)
62
63 Remove any common leading whitespace from every line in *text*.
64
65 This can be used to make triple-quoted strings line up with the left edge of the
66 display, while still presenting them in the source code in indented form.
67
68 Note that tabs and spaces are both treated as whitespace, but they are not
69 equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
Georg Brandle6bcc912008-05-12 18:05:20 +000070 common leading whitespace.
Georg Brandl116aa622007-08-15 14:28:22 +000071
72 For example::
73
74 def test():
75 # end first line with \ to avoid the empty line!
76 s = '''\
77 hello
78 world
79 '''
Collin Winterc79461b2007-09-01 23:34:30 +000080 print(repr(s)) # prints ' hello\n world\n '
81 print(repr(dedent(s))) # prints 'hello\n world\n'
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
Nick Coghlan4fae8cd2012-06-11 23:07:51 +100084.. function:: indent(text, prefix, predicate=None)
85
86 Add *prefix* to the beginning of selected lines in *text*.
87
88 Lines are separated by calling ``text.splitlines(True)``.
89
90 By default, *prefix* is added to all lines that do not consist
91 solely of whitespace (including any line endings).
92
93 For example::
94
95 >>> s = 'hello\n\n \nworld'
96 >>> indent(s, ' ')
97 ' hello\n\n \n world'
98
99 The optional *predicate* argument can be used to control which lines
100 are indented. For example, it is easy to add *prefix* to even empty
101 and whitespace-only lines::
102
103 >>> print(indent(s, '+ ', lambda line: True))
104 + hello
105 +
106 +
107 + world
108
109
Antoine Pitrou389dec82013-08-12 22:39:09 +0200110:func:`wrap`, :func:`fill` and :func:`shorten` work by creating a
111:class:`TextWrapper` instance and calling a single method on it. That
112instance is not reused, so for applications that process many text
113strings, it may be more efficient to create your own
114:class:`TextWrapper` object.
115
116Text is preferably wrapped on whitespaces and right after the hyphens in
117hyphenated words; only then will long words be broken if necessary, unless
118:attr:`TextWrapper.break_long_words` is set to false.
119
Georg Brandl7f01a132009-09-16 15:58:14 +0000120.. class:: TextWrapper(**kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122 The :class:`TextWrapper` constructor accepts a number of optional keyword
Georg Brandl7f01a132009-09-16 15:58:14 +0000123 arguments. Each keyword argument corresponds to an instance attribute, so
124 for example ::
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126 wrapper = TextWrapper(initial_indent="* ")
127
128 is the same as ::
129
130 wrapper = TextWrapper()
131 wrapper.initial_indent = "* "
132
133 You can re-use the same :class:`TextWrapper` object many times, and you can
134 change any of its options through direct assignment to instance attributes
135 between uses.
136
Benjamin Petersone41251e2008-04-25 01:59:09 +0000137 The :class:`TextWrapper` instance attributes (and keyword arguments to the
138 constructor) are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140
Benjamin Petersone41251e2008-04-25 01:59:09 +0000141 .. attribute:: width
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Benjamin Petersone41251e2008-04-25 01:59:09 +0000143 (default: ``70``) The maximum length of wrapped lines. As long as there
144 are no individual words in the input text longer than :attr:`width`,
145 :class:`TextWrapper` guarantees that no output line will be longer than
146 :attr:`width` characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 .. attribute:: expand_tabs
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Benjamin Petersone41251e2008-04-25 01:59:09 +0000151 (default: ``True``) If true, then all tab characters in *text* will be
152 expanded to spaces using the :meth:`expandtabs` method of *text*.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154
Hynek Schlawackd5272592012-05-19 13:33:11 +0200155 .. attribute:: tabsize
156
157 (default: ``8``) If :attr:`expand_tabs` is true, then all tab characters
158 in *text* will be expanded to zero or more spaces, depending on the
159 current column and the given tab size.
160
161 .. versionadded:: 3.3
162
163
Benjamin Petersone41251e2008-04-25 01:59:09 +0000164 .. attribute:: replace_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000165
Andrew Svetlov59db4012012-08-13 23:22:23 +0300166 (default: ``True``) If true, after tab expansion but before wrapping,
167 the :meth:`wrap` method will replace each whitespace character
168 with a single space. The whitespace characters replaced are
169 as follows: tab, newline, vertical tab, formfeed, and carriage
170 return (``'\t\n\v\f\r'``).
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Benjamin Petersone41251e2008-04-25 01:59:09 +0000172 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Benjamin Petersone41251e2008-04-25 01:59:09 +0000174 If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true,
175 each tab character will be replaced by a single space, which is *not*
176 the same as tab expansion.
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Terry Reedy6d2ab712010-11-23 20:17:24 +0000178 .. note::
179
180 If :attr:`replace_whitespace` is false, newlines may appear in the
181 middle of a line and cause strange output. For this reason, text should
182 be split into paragraphs (using :meth:`str.splitlines` or similar)
183 which are wrapped separately.
184
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Benjamin Petersone41251e2008-04-25 01:59:09 +0000186 .. attribute:: drop_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000187
R David Murray1585b702012-09-08 13:13:25 -0400188 (default: ``True``) If true, whitespace at the beginning and ending of
189 every line (after wrapping but before indenting) is dropped.
190 Whitespace at the beginning of the paragraph, however, is not dropped
191 if non-whitespace follows it. If whitespace being dropped takes up an
192 entire line, the whole line is dropped.
Georg Brandl116aa622007-08-15 14:28:22 +0000193
Georg Brandl116aa622007-08-15 14:28:22 +0000194
Benjamin Petersone41251e2008-04-25 01:59:09 +0000195 .. attribute:: initial_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 (default: ``''``) String that will be prepended to the first line of
R David Murray1585b702012-09-08 13:13:25 -0400198 wrapped output. Counts towards the length of the first line. The empty
199 string is not indented.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 .. attribute:: subsequent_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000203
Benjamin Petersone41251e2008-04-25 01:59:09 +0000204 (default: ``''``) String that will be prepended to all lines of wrapped
205 output except the first. Counts towards the length of each line except
206 the first.
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208
Benjamin Petersone41251e2008-04-25 01:59:09 +0000209 .. attribute:: fix_sentence_endings
Georg Brandl116aa622007-08-15 14:28:22 +0000210
Benjamin Petersone41251e2008-04-25 01:59:09 +0000211 (default: ``False``) If true, :class:`TextWrapper` attempts to detect
212 sentence endings and ensure that sentences are always separated by exactly
213 two spaces. This is generally desired for text in a monospaced font.
214 However, the sentence detection algorithm is imperfect: it assumes that a
215 sentence ending consists of a lowercase letter followed by one of ``'.'``,
216 ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
217 followed by a space. One problem with this is algorithm is that it is
218 unable to detect the difference between "Dr." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000219
Benjamin Petersone41251e2008-04-25 01:59:09 +0000220 [...] Dr. Frankenstein's monster [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000221
Benjamin Petersone41251e2008-04-25 01:59:09 +0000222 and "Spot." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Benjamin Petersone41251e2008-04-25 01:59:09 +0000224 [...] See Spot. See Spot run [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Benjamin Petersone41251e2008-04-25 01:59:09 +0000226 :attr:`fix_sentence_endings` is false by default.
Georg Brandl116aa622007-08-15 14:28:22 +0000227
Benjamin Petersone41251e2008-04-25 01:59:09 +0000228 Since the sentence detection algorithm relies on ``string.lowercase`` for
229 the definition of "lowercase letter," and a convention of using two spaces
230 after a period to separate sentences on the same line, it is specific to
231 English-language texts.
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233
Benjamin Petersone41251e2008-04-25 01:59:09 +0000234 .. attribute:: break_long_words
Georg Brandl116aa622007-08-15 14:28:22 +0000235
Benjamin Petersone41251e2008-04-25 01:59:09 +0000236 (default: ``True``) If true, then words longer than :attr:`width` will be
237 broken in order to ensure that no lines are longer than :attr:`width`. If
238 it is false, long words will not be broken, and some lines may be longer
239 than :attr:`width`. (Long words will be put on a line by themselves, in
240 order to minimize the amount by which :attr:`width` is exceeded.)
Georg Brandl116aa622007-08-15 14:28:22 +0000241
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000242
243 .. attribute:: break_on_hyphens
244
245 (default: ``True``) If true, wrapping will occur preferably on whitespaces
246 and right after hyphens in compound words, as it is customary in English.
247 If false, only whitespaces will be considered as potentially good places
248 for line breaks, but you need to set :attr:`break_long_words` to false if
249 you want truly insecable words. Default behaviour in previous versions
250 was to always allow breaking hyphenated words.
251
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000252
Antoine Pitrou389dec82013-08-12 22:39:09 +0200253 :class:`TextWrapper` also provides some public methods, analogous to the
Benjamin Petersone41251e2008-04-25 01:59:09 +0000254 module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Benjamin Petersone41251e2008-04-25 01:59:09 +0000256 .. method:: wrap(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000257
Benjamin Petersone41251e2008-04-25 01:59:09 +0000258 Wraps the single paragraph in *text* (a string) so every line is at most
259 :attr:`width` characters long. All wrapping options are taken from
R David Murray1585b702012-09-08 13:13:25 -0400260 instance attributes of the :class:`TextWrapper` instance. Returns a list
261 of output lines, without final newlines. If the wrapped output has no
262 content, the returned list is empty.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
Benjamin Petersone41251e2008-04-25 01:59:09 +0000265 .. method:: fill(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Benjamin Petersone41251e2008-04-25 01:59:09 +0000267 Wraps the single paragraph in *text*, and returns a single string
268 containing the wrapped paragraph.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Antoine Pitrou389dec82013-08-12 22:39:09 +0200270
Antoine Pitrouc5930562013-08-16 22:31:12 +0200271 .. function:: shorten(text, *, placeholder=" [...]")
Antoine Pitrou389dec82013-08-12 22:39:09 +0200272
273 Collapse and truncate the given text to fit in :attr:`width`
274 characters.
275
276 The text first has its whitespace collapsed. If it then fits in
277 :attr:`width`, it is returned as-is. Otherwise, as many words
278 as possible are joined and then the *placeholder* is appended.
279
280 .. versionadded:: 3.4