blob: 8fbfe9c1c10fa7c58081a2f9bd300ef8ff5cd6e0 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`textwrap` --- Text wrapping and filling
3=============================================
4
5.. module:: textwrap
6 :synopsis: Text wrapping and filling
7.. moduleauthor:: Greg Ward <gward@python.net>
8.. sectionauthor:: Greg Ward <gward@python.net>
9
10
11.. versionadded:: 2.3
12
13The :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
Raymond Hettingere679a372010-11-05 23:58:42 +000019.. seealso::
20
21 Latest version of the `textwrap module Python source code
22 <http://svn.python.org/view/python/branches/release27-maint/Lib/textwrap.py?view=markup>`_
Georg Brandl8ec7f652007-08-15 14:28:01 +000023
24.. function:: wrap(text[, width[, ...]])
25
26 Wraps the single paragraph in *text* (a string) so every line is at most *width*
27 characters long. Returns a list of output lines, without final newlines.
28
29 Optional keyword arguments correspond to the instance attributes of
30 :class:`TextWrapper`, documented below. *width* defaults to ``70``.
31
32
33.. function:: fill(text[, width[, ...]])
34
35 Wraps the single paragraph in *text*, and returns a single string containing the
36 wrapped paragraph. :func:`fill` is shorthand for ::
37
38 "\n".join(wrap(text, ...))
39
40 In particular, :func:`fill` accepts exactly the same keyword arguments as
41 :func:`wrap`.
42
43Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper`
44instance and calling a single method on it. That instance is not reused, so for
45applications that wrap/fill many text strings, it will be more efficient for you
46to create your own :class:`TextWrapper` object.
47
Georg Brandl6f95ae52008-05-11 10:42:28 +000048Text is preferably wrapped on whitespaces and right after the hyphens in
49hyphenated words; only then will long words be broken if necessary, unless
50:attr:`TextWrapper.break_long_words` is set to false.
51
Georg Brandl8ec7f652007-08-15 14:28:01 +000052An additional utility function, :func:`dedent`, is provided to remove
53indentation from strings that have unwanted whitespace to the left of the text.
54
55
56.. function:: dedent(text)
57
58 Remove any common leading whitespace from every line in *text*.
59
60 This can be used to make triple-quoted strings line up with the left edge of the
61 display, while still presenting them in the source code in indented form.
62
63 Note that tabs and spaces are both treated as whitespace, but they are not
64 equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
65 common leading whitespace. (This behaviour is new in Python 2.5; older versions
66 of this module incorrectly expanded tabs before searching for common leading
67 whitespace.)
68
69 For example::
70
71 def test():
72 # end first line with \ to avoid the empty line!
73 s = '''\
74 hello
75 world
76 '''
77 print repr(s) # prints ' hello\n world\n '
78 print repr(dedent(s)) # prints 'hello\n world\n'
79
80
81.. class:: TextWrapper(...)
82
83 The :class:`TextWrapper` constructor accepts a number of optional keyword
84 arguments. Each argument corresponds to one instance attribute, so for example
85 ::
86
87 wrapper = TextWrapper(initial_indent="* ")
88
89 is the same as ::
90
91 wrapper = TextWrapper()
92 wrapper.initial_indent = "* "
93
94 You can re-use the same :class:`TextWrapper` object many times, and you can
95 change any of its options through direct assignment to instance attributes
96 between uses.
97
Benjamin Petersonc7b05922008-04-25 01:29:10 +000098 The :class:`TextWrapper` instance attributes (and keyword arguments to the
99 constructor) are as follows:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
101
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000102 .. attribute:: width
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000104 (default: ``70``) The maximum length of wrapped lines. As long as there
105 are no individual words in the input text longer than :attr:`width`,
106 :class:`TextWrapper` guarantees that no output line will be longer than
107 :attr:`width` characters.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
109
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000110 .. attribute:: expand_tabs
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000112 (default: ``True``) If true, then all tab characters in *text* will be
113 expanded to spaces using the :meth:`expandtabs` method of *text*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114
115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 .. attribute:: replace_whitespace
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000118 (default: ``True``) If true, each whitespace character (as defined by
119 ``string.whitespace``) remaining after tab expansion will be replaced by a
120 single space.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000122 .. note::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000123
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000124 If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true,
125 each tab character will be replaced by a single space, which is *not*
126 the same as tab expansion.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
Terry Reedy61ff85e2010-11-23 20:28:34 +0000128 .. note::
129
130 If :attr:`replace_whitespace` is false, newlines may appear in the
131 middle of a line and cause strange output. For this reason, text should
132 be split into paragraphs (using :meth:`str.splitlines` or similar)
133 which are wrapped separately.
134
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000136 .. attribute:: drop_whitespace
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000138 (default: ``True``) If true, whitespace that, after wrapping, happens to
139 end up at the beginning or end of a line is dropped (leading whitespace in
140 the first line is always preserved, though).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000142 .. versionadded:: 2.6
143 Whitespace was always dropped in earlier versions.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000144
145
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000146 .. attribute:: initial_indent
Georg Brandl8ec7f652007-08-15 14:28:01 +0000147
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000148 (default: ``''``) String that will be prepended to the first line of
149 wrapped output. Counts towards the length of the first line.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150
151
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000152 .. attribute:: subsequent_indent
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000154 (default: ``''``) String that will be prepended to all lines of wrapped
155 output except the first. Counts towards the length of each line except
156 the first.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000157
158
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000159 .. attribute:: fix_sentence_endings
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000161 (default: ``False``) If true, :class:`TextWrapper` attempts to detect
162 sentence endings and ensure that sentences are always separated by exactly
163 two spaces. This is generally desired for text in a monospaced font.
164 However, the sentence detection algorithm is imperfect: it assumes that a
165 sentence ending consists of a lowercase letter followed by one of ``'.'``,
166 ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
167 followed by a space. One problem with this is algorithm is that it is
168 unable to detect the difference between "Dr." in ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000169
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000170 [...] Dr. Frankenstein's monster [...]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000171
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000172 and "Spot." in ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000173
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000174 [...] See Spot. See Spot run [...]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000175
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000176 :attr:`fix_sentence_endings` is false by default.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000177
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000178 Since the sentence detection algorithm relies on ``string.lowercase`` for
179 the definition of "lowercase letter," and a convention of using two spaces
180 after a period to separate sentences on the same line, it is specific to
181 English-language texts.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000182
183
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000184 .. attribute:: break_long_words
Georg Brandl8ec7f652007-08-15 14:28:01 +0000185
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000186 (default: ``True``) If true, then words longer than :attr:`width` will be
187 broken in order to ensure that no lines are longer than :attr:`width`. If
188 it is false, long words will not be broken, and some lines may be longer
189 than :attr:`width`. (Long words will be put on a line by themselves, in
190 order to minimize the amount by which :attr:`width` is exceeded.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000191
Georg Brandl6f95ae52008-05-11 10:42:28 +0000192
193 .. attribute:: break_on_hyphens
194
195 (default: ``True``) If true, wrapping will occur preferably on whitespaces
196 and right after hyphens in compound words, as it is customary in English.
197 If false, only whitespaces will be considered as potentially good places
198 for line breaks, but you need to set :attr:`break_long_words` to false if
199 you want truly insecable words. Default behaviour in previous versions
200 was to always allow breaking hyphenated words.
201
202 .. versionadded:: 2.6
203
204
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000205 :class:`TextWrapper` also provides two public methods, analogous to the
206 module-level convenience functions:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000207
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000208 .. method:: wrap(text)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000210 Wraps the single paragraph in *text* (a string) so every line is at most
211 :attr:`width` characters long. All wrapping options are taken from
212 instance attributes of the :class:`TextWrapper` instance. Returns a list
213 of output lines, without final newlines.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000214
215
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000216 .. method:: fill(text)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000218 Wraps the single paragraph in *text*, and returns a single string
219 containing the wrapped paragraph.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000220