blob: 350354cd9e821c13e32533f171928818cfc86754 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
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
17
18.. function:: wrap(text[, width[, ...]])
19
20 Wraps the single paragraph in *text* (a string) so every line is at most *width*
21 characters long. Returns a list of output lines, without final newlines.
22
23 Optional keyword arguments correspond to the instance attributes of
24 :class:`TextWrapper`, documented below. *width* defaults to ``70``.
25
26
27.. function:: fill(text[, width[, ...]])
28
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
42An additional utility function, :func:`dedent`, is provided to remove
43indentation from strings that have unwanted whitespace to the left of the text.
44
45
46.. function:: dedent(text)
47
48 Remove any common leading whitespace from every line in *text*.
49
50 This can be used to make triple-quoted strings line up with the left edge of the
51 display, while still presenting them in the source code in indented form.
52
53 Note that tabs and spaces are both treated as whitespace, but they are not
54 equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no
55 common leading whitespace. (This behaviour is new in Python 2.5; older versions
56 of this module incorrectly expanded tabs before searching for common leading
57 whitespace.)
58
59 For example::
60
61 def test():
62 # end first line with \ to avoid the empty line!
63 s = '''\
64 hello
65 world
66 '''
Collin Winterc79461b2007-09-01 23:34:30 +000067 print(repr(s)) # prints ' hello\n world\n '
68 print(repr(dedent(s))) # prints 'hello\n world\n'
Georg Brandl116aa622007-08-15 14:28:22 +000069
70
71.. class:: TextWrapper(...)
72
73 The :class:`TextWrapper` constructor accepts a number of optional keyword
74 arguments. Each argument corresponds to one instance attribute, so for example
75 ::
76
77 wrapper = TextWrapper(initial_indent="* ")
78
79 is the same as ::
80
81 wrapper = TextWrapper()
82 wrapper.initial_indent = "* "
83
84 You can re-use the same :class:`TextWrapper` object many times, and you can
85 change any of its options through direct assignment to instance attributes
86 between uses.
87
88The :class:`TextWrapper` instance attributes (and keyword arguments to the
89constructor) are as follows:
90
91
92.. attribute:: TextWrapper.width
93
94 (default: ``70``) The maximum length of wrapped lines. As long as there are no
95 individual words in the input text longer than :attr:`width`,
96 :class:`TextWrapper` guarantees that no output line will be longer than
97 :attr:`width` characters.
98
99
100.. attribute:: TextWrapper.expand_tabs
101
102 (default: ``True``) If true, then all tab characters in *text* will be expanded
103 to spaces using the :meth:`expandtabs` method of *text*.
104
105
106.. attribute:: TextWrapper.replace_whitespace
107
108 (default: ``True``) If true, each whitespace character (as defined by
109 ``string.whitespace``) remaining after tab expansion will be replaced by a
110 single space.
111
112 .. note::
113
114 If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, each tab
115 character will be replaced by a single space, which is *not* the same as tab
116 expansion.
117
118
119.. attribute:: TextWrapper.drop_whitespace
120
121 (default: ``True``) If true, whitespace that, after wrapping, happens to end up
122 at the beginning or end of a line is dropped (leading whitespace in the first
123 line is always preserved, though).
124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. attribute:: TextWrapper.initial_indent
127
128 (default: ``''``) String that will be prepended to the first line of wrapped
129 output. Counts towards the length of the first line.
130
131
132.. attribute:: TextWrapper.subsequent_indent
133
134 (default: ``''``) String that will be prepended to all lines of wrapped output
135 except the first. Counts towards the length of each line except the first.
136
137
138.. attribute:: TextWrapper.fix_sentence_endings
139
140 (default: ``False``) If true, :class:`TextWrapper` attempts to detect sentence
141 endings and ensure that sentences are always separated by exactly two spaces.
142 This is generally desired for text in a monospaced font. However, the sentence
143 detection algorithm is imperfect: it assumes that a sentence ending consists of
144 a lowercase letter followed by one of ``'.'``, ``'!'``, or ``'?'``, possibly
145 followed by one of ``'"'`` or ``"'"``, followed by a space. One problem with
146 this is algorithm is that it is unable to detect the difference between "Dr." in
147 ::
148
149 [...] Dr. Frankenstein's monster [...]
150
151 and "Spot." in ::
152
153 [...] See Spot. See Spot run [...]
154
155 :attr:`fix_sentence_endings` is false by default.
156
157 Since the sentence detection algorithm relies on ``string.lowercase`` for the
158 definition of "lowercase letter," and a convention of using two spaces after
159 a period to separate sentences on the same line, it is specific to
160 English-language texts.
161
162
163.. attribute:: TextWrapper.break_long_words
164
165 (default: ``True``) If true, then words longer than :attr:`width` will be broken
166 in order to ensure that no lines are longer than :attr:`width`. If it is false,
167 long words will not be broken, and some lines may be longer than :attr:`width`.
168 (Long words will be put on a line by themselves, in order to minimize the amount
169 by which :attr:`width` is exceeded.)
170
171:class:`TextWrapper` also provides two public methods, analogous to the
172module-level convenience functions:
173
174
175.. method:: TextWrapper.wrap(text)
176
177 Wraps the single paragraph in *text* (a string) so every line is at most
178 :attr:`width` characters long. All wrapping options are taken from instance
179 attributes of the :class:`TextWrapper` instance. Returns a list of output lines,
180 without final newlines.
181
182
183.. method:: TextWrapper.fill(text)
184
185 Wraps the single paragraph in *text*, and returns a single string containing the
186 wrapped paragraph.
187