blob: 74e5b2ecf057d7cff8a15e272458834711a7bba6 [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
Georg Brandl116aa622007-08-15 14:28:22 +000011The :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,
13and a utility function :func:`dedent`. If you're just wrapping or filling one
14or two text strings, the convenience functions should be good enough;
15otherwise, you should use an instance of :class:`TextWrapper` for efficiency.
16
Georg Brandl7f01a132009-09-16 15:58:14 +000017.. function:: wrap(text, width=70, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000018
Georg Brandl7f01a132009-09-16 15:58:14 +000019 Wraps the single paragraph in *text* (a string) so every line is at most
20 *width* characters long. Returns a list of output lines, without final
21 newlines.
Georg Brandl116aa622007-08-15 14:28:22 +000022
23 Optional keyword arguments correspond to the instance attributes of
24 :class:`TextWrapper`, documented below. *width* defaults to ``70``.
25
26
Georg Brandl7f01a132009-09-16 15:58:14 +000027.. function:: fill(text, width=70, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000028
29 Wraps the single paragraph in *text*, and returns a single string containing the
30 wrapped paragraph. :func:`fill` is shorthand for ::
31
32 "\n".join(wrap(text, ...))
33
34 In particular, :func:`fill` accepts exactly the same keyword arguments as
35 :func:`wrap`.
36
37Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper`
38instance and calling a single method on it. That instance is not reused, so for
39applications that wrap/fill many text strings, it will be more efficient for you
40to create your own :class:`TextWrapper` object.
41
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000042Text is preferably wrapped on whitespaces and right after the hyphens in
43hyphenated words; only then will long words be broken if necessary, unless
44:attr:`TextWrapper.break_long_words` is set to false.
45
Georg Brandl116aa622007-08-15 14:28:22 +000046An additional utility function, :func:`dedent`, is provided to remove
47indentation from strings that have unwanted whitespace to the left of the text.
48
49
50.. function:: dedent(text)
51
52 Remove any common leading whitespace from every line in *text*.
53
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.
56
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
Georg Brandle6bcc912008-05-12 18:05:20 +000059 common leading whitespace.
Georg Brandl116aa622007-08-15 14:28:22 +000060
61 For example::
62
63 def test():
64 # end first line with \ to avoid the empty line!
65 s = '''\
66 hello
67 world
68 '''
Collin Winterc79461b2007-09-01 23:34:30 +000069 print(repr(s)) # prints ' hello\n world\n '
70 print(repr(dedent(s))) # prints 'hello\n world\n'
Georg Brandl116aa622007-08-15 14:28:22 +000071
72
Georg Brandl7f01a132009-09-16 15:58:14 +000073.. class:: TextWrapper(**kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000074
75 The :class:`TextWrapper` constructor accepts a number of optional keyword
Georg Brandl7f01a132009-09-16 15:58:14 +000076 arguments. Each keyword argument corresponds to an instance attribute, so
77 for example ::
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 wrapper = TextWrapper(initial_indent="* ")
80
81 is the same as ::
82
83 wrapper = TextWrapper()
84 wrapper.initial_indent = "* "
85
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
88 between uses.
89
Benjamin Petersone41251e2008-04-25 01:59:09 +000090 The :class:`TextWrapper` instance attributes (and keyword arguments to the
91 constructor) are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 .. attribute:: width
Georg Brandl116aa622007-08-15 14:28:22 +000095
Benjamin Petersone41251e2008-04-25 01:59:09 +000096 (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.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101
Benjamin Petersone41251e2008-04-25 01:59:09 +0000102 .. attribute:: expand_tabs
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 (default: ``True``) If true, then all tab characters in *text* will be
105 expanded to spaces using the :meth:`expandtabs` method of *text*.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
Benjamin Petersone41251e2008-04-25 01:59:09 +0000108 .. attribute:: replace_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 (default: ``True``) If true, each whitespace character (as defined by
111 ``string.whitespace``) remaining after tab expansion will be replaced by a
112 single space.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 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.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Terry Reedy6d2ab712010-11-23 20:17:24 +0000120 .. note::
121
122 If :attr:`replace_whitespace` is false, newlines may appear in the
123 middle of a line and cause strange output. For this reason, text should
124 be split into paragraphs (using :meth:`str.splitlines` or similar)
125 which are wrapped separately.
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 .. attribute:: drop_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000129
Benjamin Petersone41251e2008-04-25 01:59:09 +0000130 (default: ``True``) If true, whitespace that, after wrapping, happens to
131 end up at the beginning or end of a line is dropped (leading whitespace in
132 the first line is always preserved, though).
Georg Brandl116aa622007-08-15 14:28:22 +0000133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Benjamin Petersone41251e2008-04-25 01:59:09 +0000135 .. attribute:: initial_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000136
Benjamin Petersone41251e2008-04-25 01:59:09 +0000137 (default: ``''``) String that will be prepended to the first line of
138 wrapped output. Counts towards the length of the first line.
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140
Benjamin Petersone41251e2008-04-25 01:59:09 +0000141 .. attribute:: subsequent_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Benjamin Petersone41251e2008-04-25 01:59:09 +0000143 (default: ``''``) String that will be prepended to all lines of wrapped
144 output except the first. Counts towards the length of each line except
145 the first.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147
Benjamin Petersone41251e2008-04-25 01:59:09 +0000148 .. attribute:: fix_sentence_endings
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Benjamin Petersone41251e2008-04-25 01:59:09 +0000150 (default: ``False``) If true, :class:`TextWrapper` attempts to detect
151 sentence endings and ensure that sentences are always separated by exactly
152 two spaces. This is generally desired for text in a monospaced font.
153 However, the sentence detection algorithm is imperfect: it assumes that a
154 sentence ending consists of a lowercase letter followed by one of ``'.'``,
155 ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
156 followed by a space. One problem with this is algorithm is that it is
157 unable to detect the difference between "Dr." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Benjamin Petersone41251e2008-04-25 01:59:09 +0000159 [...] Dr. Frankenstein's monster [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Benjamin Petersone41251e2008-04-25 01:59:09 +0000161 and "Spot." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Benjamin Petersone41251e2008-04-25 01:59:09 +0000163 [...] See Spot. See Spot run [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000164
Benjamin Petersone41251e2008-04-25 01:59:09 +0000165 :attr:`fix_sentence_endings` is false by default.
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 Since the sentence detection algorithm relies on ``string.lowercase`` for
168 the definition of "lowercase letter," and a convention of using two spaces
169 after a period to separate sentences on the same line, it is specific to
170 English-language texts.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
Benjamin Petersone41251e2008-04-25 01:59:09 +0000173 .. attribute:: break_long_words
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Benjamin Petersone41251e2008-04-25 01:59:09 +0000175 (default: ``True``) If true, then words longer than :attr:`width` will be
176 broken in order to ensure that no lines are longer than :attr:`width`. If
177 it is false, long words will not be broken, and some lines may be longer
178 than :attr:`width`. (Long words will be put on a line by themselves, in
179 order to minimize the amount by which :attr:`width` is exceeded.)
Georg Brandl116aa622007-08-15 14:28:22 +0000180
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000181
182 .. attribute:: break_on_hyphens
183
184 (default: ``True``) If true, wrapping will occur preferably on whitespaces
185 and right after hyphens in compound words, as it is customary in English.
186 If false, only whitespaces will be considered as potentially good places
187 for line breaks, but you need to set :attr:`break_long_words` to false if
188 you want truly insecable words. Default behaviour in previous versions
189 was to always allow breaking hyphenated words.
190
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000191
Benjamin Petersone41251e2008-04-25 01:59:09 +0000192 :class:`TextWrapper` also provides two public methods, analogous to the
193 module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000194
Benjamin Petersone41251e2008-04-25 01:59:09 +0000195 .. method:: wrap(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 Wraps the single paragraph in *text* (a string) so every line is at most
198 :attr:`width` characters long. All wrapping options are taken from
199 instance attributes of the :class:`TextWrapper` instance. Returns a list
200 of output lines, without final newlines.
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202
Benjamin Petersone41251e2008-04-25 01:59:09 +0000203 .. method:: fill(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000204
Benjamin Petersone41251e2008-04-25 01:59:09 +0000205 Wraps the single paragraph in *text*, and returns a single string
206 containing the wrapped paragraph.
Georg Brandl116aa622007-08-15 14:28:22 +0000207