blob: f8817013b5faee340d682feae70fc1b065d0c4fe [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
9
Georg Brandl116aa622007-08-15 14:28:22 +000010The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and
11:func:`fill`, as well as :class:`TextWrapper`, the class that does all the work,
12and a utility function :func:`dedent`. If you're just wrapping or filling one
13or two text strings, the convenience functions should be good enough;
14otherwise, you should use an instance of :class:`TextWrapper` for efficiency.
15
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000016.. seealso::
17
18 Latest version of the :source:`textwrap module Python source code
19 <Lib/textwrap.py>`
Georg Brandl116aa622007-08-15 14:28:22 +000020
Georg Brandl7f01a132009-09-16 15:58:14 +000021.. function:: wrap(text, width=70, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000022
Georg Brandl7f01a132009-09-16 15:58:14 +000023 Wraps the single paragraph in *text* (a string) so every line is at most
24 *width* characters long. Returns a list of output lines, without final
25 newlines.
Georg Brandl116aa622007-08-15 14:28:22 +000026
27 Optional keyword arguments correspond to the instance attributes of
28 :class:`TextWrapper`, documented below. *width* defaults to ``70``.
29
30
Georg Brandl7f01a132009-09-16 15:58:14 +000031.. function:: fill(text, width=70, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000032
33 Wraps the single paragraph in *text*, and returns a single string containing the
34 wrapped paragraph. :func:`fill` is shorthand for ::
35
36 "\n".join(wrap(text, ...))
37
38 In particular, :func:`fill` accepts exactly the same keyword arguments as
39 :func:`wrap`.
40
41Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper`
42instance and calling a single method on it. That instance is not reused, so for
43applications that wrap/fill many text strings, it will be more efficient for you
44to create your own :class:`TextWrapper` object.
45
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000046Text is preferably wrapped on whitespaces and right after the hyphens in
47hyphenated words; only then will long words be broken if necessary, unless
48:attr:`TextWrapper.break_long_words` is set to false.
49
Georg Brandl116aa622007-08-15 14:28:22 +000050An additional utility function, :func:`dedent`, is provided to remove
51indentation from strings that have unwanted whitespace to the left of the text.
52
53
54.. function:: dedent(text)
55
56 Remove any common leading whitespace from every line in *text*.
57
58 This can be used to make triple-quoted strings line up with the left edge of the
59 display, while still presenting them in the source code in indented form.
60
61 Note that tabs and spaces are both treated as whitespace, but they are not
62 equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
Georg Brandle6bcc912008-05-12 18:05:20 +000063 common leading whitespace.
Georg Brandl116aa622007-08-15 14:28:22 +000064
65 For example::
66
67 def test():
68 # end first line with \ to avoid the empty line!
69 s = '''\
70 hello
71 world
72 '''
Collin Winterc79461b2007-09-01 23:34:30 +000073 print(repr(s)) # prints ' hello\n world\n '
74 print(repr(dedent(s))) # prints 'hello\n world\n'
Georg Brandl116aa622007-08-15 14:28:22 +000075
76
Georg Brandl7f01a132009-09-16 15:58:14 +000077.. class:: TextWrapper(**kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 The :class:`TextWrapper` constructor accepts a number of optional keyword
Georg Brandl7f01a132009-09-16 15:58:14 +000080 arguments. Each keyword argument corresponds to an instance attribute, so
81 for example ::
Georg Brandl116aa622007-08-15 14:28:22 +000082
83 wrapper = TextWrapper(initial_indent="* ")
84
85 is the same as ::
86
87 wrapper = TextWrapper()
88 wrapper.initial_indent = "* "
89
90 You can re-use the same :class:`TextWrapper` object many times, and you can
91 change any of its options through direct assignment to instance attributes
92 between uses.
93
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 The :class:`TextWrapper` instance attributes (and keyword arguments to the
95 constructor) are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +000096
97
Benjamin Petersone41251e2008-04-25 01:59:09 +000098 .. attribute:: width
Georg Brandl116aa622007-08-15 14:28:22 +000099
Benjamin Petersone41251e2008-04-25 01:59:09 +0000100 (default: ``70``) The maximum length of wrapped lines. As long as there
101 are no individual words in the input text longer than :attr:`width`,
102 :class:`TextWrapper` guarantees that no output line will be longer than
103 :attr:`width` characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
Benjamin Petersone41251e2008-04-25 01:59:09 +0000106 .. attribute:: expand_tabs
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Benjamin Petersone41251e2008-04-25 01:59:09 +0000108 (default: ``True``) If true, then all tab characters in *text* will be
109 expanded to spaces using the :meth:`expandtabs` method of *text*.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111
Benjamin Petersone41251e2008-04-25 01:59:09 +0000112 .. attribute:: replace_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 (default: ``True``) If true, each whitespace character (as defined by
115 ``string.whitespace``) remaining after tab expansion will be replaced by a
116 single space.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Benjamin Petersone41251e2008-04-25 01:59:09 +0000120 If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true,
121 each tab character will be replaced by a single space, which is *not*
122 the same as tab expansion.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 .. attribute:: drop_whitespace
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Benjamin Petersone41251e2008-04-25 01:59:09 +0000127 (default: ``True``) If true, whitespace that, after wrapping, happens to
128 end up at the beginning or end of a line is dropped (leading whitespace in
129 the first line is always preserved, though).
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Georg Brandl116aa622007-08-15 14:28:22 +0000131
Benjamin Petersone41251e2008-04-25 01:59:09 +0000132 .. attribute:: initial_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000133
Benjamin Petersone41251e2008-04-25 01:59:09 +0000134 (default: ``''``) String that will be prepended to the first line of
135 wrapped output. Counts towards the length of the first line.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 .. attribute:: subsequent_indent
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 (default: ``''``) String that will be prepended to all lines of wrapped
141 output except the first. Counts towards the length of each line except
142 the first.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144
Benjamin Petersone41251e2008-04-25 01:59:09 +0000145 .. attribute:: fix_sentence_endings
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 (default: ``False``) If true, :class:`TextWrapper` attempts to detect
148 sentence endings and ensure that sentences are always separated by exactly
149 two spaces. This is generally desired for text in a monospaced font.
150 However, the sentence detection algorithm is imperfect: it assumes that a
151 sentence ending consists of a lowercase letter followed by one of ``'.'``,
152 ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``,
153 followed by a space. One problem with this is algorithm is that it is
154 unable to detect the difference between "Dr." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 [...] Dr. Frankenstein's monster [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Benjamin Petersone41251e2008-04-25 01:59:09 +0000158 and "Spot." in ::
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 [...] See Spot. See Spot run [...]
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Benjamin Petersone41251e2008-04-25 01:59:09 +0000162 :attr:`fix_sentence_endings` is false by default.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Benjamin Petersone41251e2008-04-25 01:59:09 +0000164 Since the sentence detection algorithm relies on ``string.lowercase`` for
165 the definition of "lowercase letter," and a convention of using two spaces
166 after a period to separate sentences on the same line, it is specific to
167 English-language texts.
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169
Benjamin Petersone41251e2008-04-25 01:59:09 +0000170 .. attribute:: break_long_words
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Benjamin Petersone41251e2008-04-25 01:59:09 +0000172 (default: ``True``) If true, then words longer than :attr:`width` will be
173 broken in order to ensure that no lines are longer than :attr:`width`. If
174 it is false, long words will not be broken, and some lines may be longer
175 than :attr:`width`. (Long words will be put on a line by themselves, in
176 order to minimize the amount by which :attr:`width` is exceeded.)
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000178
179 .. attribute:: break_on_hyphens
180
181 (default: ``True``) If true, wrapping will occur preferably on whitespaces
182 and right after hyphens in compound words, as it is customary in English.
183 If false, only whitespaces will be considered as potentially good places
184 for line breaks, but you need to set :attr:`break_long_words` to false if
185 you want truly insecable words. Default behaviour in previous versions
186 was to always allow breaking hyphenated words.
187
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000188
Benjamin Petersone41251e2008-04-25 01:59:09 +0000189 :class:`TextWrapper` also provides two public methods, analogous to the
190 module-level convenience functions:
Georg Brandl116aa622007-08-15 14:28:22 +0000191
Benjamin Petersone41251e2008-04-25 01:59:09 +0000192 .. method:: wrap(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000193
Benjamin Petersone41251e2008-04-25 01:59:09 +0000194 Wraps the single paragraph in *text* (a string) so every line is at most
195 :attr:`width` characters long. All wrapping options are taken from
196 instance attributes of the :class:`TextWrapper` instance. Returns a list
197 of output lines, without final newlines.
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199
Benjamin Petersone41251e2008-04-25 01:59:09 +0000200 .. method:: fill(text)
Georg Brandl116aa622007-08-15 14:28:22 +0000201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 Wraps the single paragraph in *text*, and returns a single string
203 containing the wrapped paragraph.
Georg Brandl116aa622007-08-15 14:28:22 +0000204