blob: 3c1ecf60b6158763738f2576ab95df41992012b7 [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,
15and a utility function :func:`dedent`. If you're just wrapping or filling one
16or 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
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
42Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper`
43instance and calling a single method on it. That instance is not reused, so for
44applications that wrap/fill many text strings, it will be more efficient for you
45to create your own :class:`TextWrapper` object.
46
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000047Text is preferably wrapped on whitespaces and right after the hyphens in
48hyphenated words; only then will long words be broken if necessary, unless
49:attr:`TextWrapper.break_long_words` is set to false.
50
Georg Brandl116aa622007-08-15 14:28:22 +000051An additional utility function, :func:`dedent`, is provided to remove
52indentation from strings that have unwanted whitespace to the left of the text.
53
54
55.. function:: dedent(text)
56
57 Remove any common leading whitespace from every line in *text*.
58
59 This can be used to make triple-quoted strings line up with the left edge of the
60 display, while still presenting them in the source code in indented form.
61
62 Note that tabs and spaces are both treated as whitespace, but they are not
63 equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
Georg Brandle6bcc912008-05-12 18:05:20 +000064 common leading whitespace.
Georg Brandl116aa622007-08-15 14:28:22 +000065
66 For example::
67
68 def test():
69 # end first line with \ to avoid the empty line!
70 s = '''\
71 hello
72 world
73 '''
Collin Winterc79461b2007-09-01 23:34:30 +000074 print(repr(s)) # prints ' hello\n world\n '
75 print(repr(dedent(s))) # prints 'hello\n world\n'
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
Georg Brandl7f01a132009-09-16 15:58:14 +000078.. class:: TextWrapper(**kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000079
80 The :class:`TextWrapper` constructor accepts a number of optional keyword
Georg Brandl7f01a132009-09-16 15:58:14 +000081 arguments. Each keyword argument corresponds to an instance attribute, so
82 for example ::
Georg Brandl116aa622007-08-15 14:28:22 +000083
84 wrapper = TextWrapper(initial_indent="* ")
85
86 is the same as ::
87
88 wrapper = TextWrapper()
89 wrapper.initial_indent = "* "
90
91 You can re-use the same :class:`TextWrapper` object many times, and you can
92 change any of its options through direct assignment to instance attributes
93 between uses.
94
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 The :class:`TextWrapper` instance attributes (and keyword arguments to the
96 constructor) are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 .. attribute:: width
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 (default: ``70``) The maximum length of wrapped lines. As long as there
102 are no individual words in the input text longer than :attr:`width`,
103 :class:`TextWrapper` guarantees that no output line will be longer than
104 :attr:`width` characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 .. attribute:: expand_tabs
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 (default: ``True``) If true, then all tab characters in *text* will be
110 expanded to spaces using the :meth:`expandtabs` method of *text*.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 .. attribute:: replace_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Andrew Svetlov59db4012012-08-13 23:22:23 +0300115 (default: ``True``) If true, after tab expansion but before wrapping,
116 the :meth:`wrap` method will replace each whitespace character
117 with a single space. The whitespace characters replaced are
118 as follows: tab, newline, vertical tab, formfeed, and carriage
119 return (``'\t\n\v\f\r'``).
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Benjamin Petersone41251e2008-04-25 01:59:09 +0000121 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true,
124 each tab character will be replaced by a single space, which is *not*
125 the same as tab expansion.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Terry Reedy6d2ab712010-11-23 20:17:24 +0000127 .. note::
128
129 If :attr:`replace_whitespace` is false, newlines may appear in the
130 middle of a line and cause strange output. For this reason, text should
131 be split into paragraphs (using :meth:`str.splitlines` or similar)
132 which are wrapped separately.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Benjamin Petersone41251e2008-04-25 01:59:09 +0000135 .. attribute:: drop_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000136
R David Murray1585b702012-09-08 13:13:25 -0400137 (default: ``True``) If true, whitespace at the beginning and ending of
138 every line (after wrapping but before indenting) is dropped.
139 Whitespace at the beginning of the paragraph, however, is not dropped
140 if non-whitespace follows it. If whitespace being dropped takes up an
141 entire line, the whole line is dropped.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 .. attribute:: initial_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 (default: ``''``) String that will be prepended to the first line of
R David Murray1585b702012-09-08 13:13:25 -0400147 wrapped output. Counts towards the length of the first line. The empty
148 string is not indented.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150
Benjamin Petersone41251e2008-04-25 01:59:09 +0000151 .. attribute:: subsequent_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Benjamin Petersone41251e2008-04-25 01:59:09 +0000153 (default: ``''``) String that will be prepended to all lines of wrapped
154 output except the first. Counts towards the length of each line except
155 the first.
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157
Benjamin Petersone41251e2008-04-25 01:59:09 +0000158 .. attribute:: fix_sentence_endings
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 (default: ``False``) If true, :class:`TextWrapper` attempts to detect
161 sentence endings and ensure that sentences are always separated by exactly
162 two spaces. This is generally desired for text in a monospaced font.
163 However, the sentence detection algorithm is imperfect: it assumes that a
164 sentence ending consists of a lowercase letter followed by one of ``'.'``,
165 ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
166 followed by a space. One problem with this is algorithm is that it is
167 unable to detect the difference between "Dr." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Benjamin Petersone41251e2008-04-25 01:59:09 +0000169 [...] Dr. Frankenstein's monster [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Benjamin Petersone41251e2008-04-25 01:59:09 +0000171 and "Spot." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Benjamin Petersone41251e2008-04-25 01:59:09 +0000173 [...] See Spot. See Spot run [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Benjamin Petersone41251e2008-04-25 01:59:09 +0000175 :attr:`fix_sentence_endings` is false by default.
Georg Brandl116aa622007-08-15 14:28:22 +0000176
Benjamin Petersone41251e2008-04-25 01:59:09 +0000177 Since the sentence detection algorithm relies on ``string.lowercase`` for
178 the definition of "lowercase letter," and a convention of using two spaces
179 after a period to separate sentences on the same line, it is specific to
180 English-language texts.
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182
Benjamin Petersone41251e2008-04-25 01:59:09 +0000183 .. attribute:: break_long_words
Georg Brandl116aa622007-08-15 14:28:22 +0000184
Benjamin Petersone41251e2008-04-25 01:59:09 +0000185 (default: ``True``) If true, then words longer than :attr:`width` will be
186 broken in order to ensure that no lines are longer than :attr:`width`. If
187 it is false, long words will not be broken, and some lines may be longer
188 than :attr:`width`. (Long words will be put on a line by themselves, in
189 order to minimize the amount by which :attr:`width` is exceeded.)
Georg Brandl116aa622007-08-15 14:28:22 +0000190
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000191
192 .. attribute:: break_on_hyphens
193
194 (default: ``True``) If true, wrapping will occur preferably on whitespaces
195 and right after hyphens in compound words, as it is customary in English.
196 If false, only whitespaces will be considered as potentially good places
197 for line breaks, but you need to set :attr:`break_long_words` to false if
198 you want truly insecable words. Default behaviour in previous versions
199 was to always allow breaking hyphenated words.
200
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 :class:`TextWrapper` also provides two public methods, analogous to the
203 module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000204
Benjamin Petersone41251e2008-04-25 01:59:09 +0000205 .. method:: wrap(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Benjamin Petersone41251e2008-04-25 01:59:09 +0000207 Wraps the single paragraph in *text* (a string) so every line is at most
208 :attr:`width` characters long. All wrapping options are taken from
R David Murray1585b702012-09-08 13:13:25 -0400209 instance attributes of the :class:`TextWrapper` instance. Returns a list
210 of output lines, without final newlines. If the wrapped output has no
211 content, the returned list is empty.
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213
Benjamin Petersone41251e2008-04-25 01:59:09 +0000214 .. method:: fill(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000215
Benjamin Petersone41251e2008-04-25 01:59:09 +0000216 Wraps the single paragraph in *text*, and returns a single string
217 containing the wrapped paragraph.
Georg Brandl116aa622007-08-15 14:28:22 +0000218