blob: 8ea25a87a6920e8a7f1825e870e85af3b220410f [file] [log] [blame]
Greg Wardae64f3ad2002-06-29 02:38:50 +00001\section{\module{textwrap} ---
2 Text wrapping and filling}
3
4\declaremodule{standard}{textwrap}
5\modulesynopsis{Text wrapping and filling}
6\moduleauthor{Greg Ward}{gward@python.net}
7\sectionauthor{Greg Ward}{gward@python.net}
8
9\versionadded{2.3}
10
11The \module{textwrap} module provides two convenience functions,
12\function{wrap()} and \function{fill()}, as well as
Greg Ward8f6329c2003-05-08 02:09:49 +000013\class{TextWrapper}, the class that does all the work, and a utility function
14\function{dedent()}. If you're just wrapping or filling one or two
15text strings, the convenience functions should be good enough; otherwise,
16you should use an instance of \class{TextWrapper} for efficiency.
Greg Wardae64f3ad2002-06-29 02:38:50 +000017
Fred Drakeca23ee22002-07-02 20:37:12 +000018\begin{funcdesc}{wrap}{text\optional{, width\optional{, \moreargs}}}
Greg Wardae64f3ad2002-06-29 02:38:50 +000019Wraps the single paragraph in \var{text} (a string) so every line is at
20most \var{width} characters long. Returns a list of output lines,
21without final newlines.
22
23Optional keyword arguments correspond to the instance attributes of
Fred Drakeca23ee22002-07-02 20:37:12 +000024\class{TextWrapper}, documented below. \var{width} defaults to
25\code{70}.
Greg Wardae64f3ad2002-06-29 02:38:50 +000026\end{funcdesc}
27
Fred Drakeca23ee22002-07-02 20:37:12 +000028\begin{funcdesc}{fill}{text\optional{, width\optional{, \moreargs}}}
Greg Wardae64f3ad2002-06-29 02:38:50 +000029Wraps the single paragraph in \var{text}, and returns a single string
30containing the wrapped paragraph. \function{fill()} is shorthand for
31\begin{verbatim}
32"\n".join(wrap(text, ...))
33\end{verbatim}
34
35In particular, \function{fill()} accepts exactly the same keyword
36arguments as \function{wrap()}.
37\end{funcdesc}
38
39Both \function{wrap()} and \function{fill()} work by creating a
40\class{TextWrapper} instance and calling a single method on it. That
41instance is not reused, so for applications that wrap/fill many text
42strings, it will be more efficient for you to create your own
43\class{TextWrapper} object.
44
Greg Ward8f6329c2003-05-08 02:09:49 +000045An additional utility function, \function{dedent()}, is provided to
46remove indentation from strings that have unwanted whitespace to the
47left of the text.
48
49\begin{funcdesc}{dedent}{text}
Greg Ward7f547402006-06-11 00:40:49 +000050Remove any common leading whitespace from every line in \var{text}.
Greg Ward8f6329c2003-05-08 02:09:49 +000051
Greg Ward7f547402006-06-11 00:40:49 +000052This can be used to make triple-quoted strings line up with the left
53edge of the display, while still presenting them in the source code
54in indented form.
55
56Note that tabs and spaces are both treated as whitespace, but they are
Georg Brandl52013642007-03-31 11:54:58 +000057not equal: the lines \code{" {} hello"} and \code{"\e thello"}
Greg Ward7f547402006-06-11 00:40:49 +000058are considered to have no common leading whitespace. (This behaviour is
59new in Python 2.5; older versions of this module incorrectly expanded
60tabs before searching for common leading whitespace.)
Greg Ward8f6329c2003-05-08 02:09:49 +000061
62For example:
63\begin{verbatim}
64def test():
65 # end first line with \ to avoid the empty line!
66 s = '''\
Greg Ward86e17902003-05-08 02:12:35 +000067 hello
68 world
Greg Ward8f6329c2003-05-08 02:09:49 +000069 '''
Greg Ward86e17902003-05-08 02:12:35 +000070 print repr(s) # prints ' hello\n world\n '
71 print repr(dedent(s)) # prints 'hello\n world\n'
Greg Ward8f6329c2003-05-08 02:09:49 +000072\end{verbatim}
73\end{funcdesc}
74
Greg Ward285f4a72002-07-02 21:48:12 +000075\begin{classdesc}{TextWrapper}{...}
76The \class{TextWrapper} constructor accepts a number of optional
77keyword arguments. Each argument corresponds to one instance attribute,
78so for example
Greg Wardae64f3ad2002-06-29 02:38:50 +000079\begin{verbatim}
80wrapper = TextWrapper(initial_indent="* ")
81\end{verbatim}
82is the same as
83\begin{verbatim}
84wrapper = TextWrapper()
85wrapper.initial_indent = "* "
86\end{verbatim}
87
88You can re-use the same \class{TextWrapper} object many times, and you
89can change any of its options through direct assignment to instance
Fred Drakeca23ee22002-07-02 20:37:12 +000090attributes between uses.
91\end{classdesc}
Greg Wardae64f3ad2002-06-29 02:38:50 +000092
Greg Ward285f4a72002-07-02 21:48:12 +000093The \class{TextWrapper} instance attributes (and keyword arguments to
94the constructor) are as follows:
Fred Drakeca23ee22002-07-02 20:37:12 +000095
Greg Ward285f4a72002-07-02 21:48:12 +000096\begin{memberdesc}{width}
Fred Drake228f6e42002-07-03 05:08:48 +000097(default: \code{70}) The maximum length of wrapped lines. As long as
98there are no individual words in the input text longer than
99\member{width}, \class{TextWrapper} guarantees that no output line
100will be longer than \member{width} characters.
Greg Ward285f4a72002-07-02 21:48:12 +0000101\end{memberdesc}
Fred Drakeca23ee22002-07-02 20:37:12 +0000102
103\begin{memberdesc}{expand_tabs}
Greg Ward285f4a72002-07-02 21:48:12 +0000104(default: \code{True}) If true, then all tab characters in \var{text}
Brett Cannon222d5b42004-12-11 09:53:52 +0000105will be expanded to spaces using the \method{expandtabs()} method of
Greg Wardae64f3ad2002-06-29 02:38:50 +0000106\var{text}.
107\end{memberdesc}
108
Fred Drakeca23ee22002-07-02 20:37:12 +0000109\begin{memberdesc}{replace_whitespace}
Fred Drake228f6e42002-07-03 05:08:48 +0000110(default: \code{True}) If true, each whitespace character (as defined
111by \code{string.whitespace}) remaining after tab expansion will be
112replaced by a single space. \note{If \member{expand_tabs} is false
113and \member{replace_whitespace} is true, each tab character will be
114replaced by a single space, which is \emph{not} the same as tab
115expansion.}
Greg Wardae64f3ad2002-06-29 02:38:50 +0000116\end{memberdesc}
117
Georg Brandl9e6b4702007-03-13 18:15:41 +0000118\begin{memberdesc}{drop_whitespace}
119(default: \code{True}) If true, whitespace that, after wrapping, happens
120to end up at the beginning or end of a line is dropped (leading whitespace
121in the first line is always preserved, though).
122\versionadded[Whitespace was always dropped in earlier versions]{2.6}
123\end{memberdesc}
124
Fred Drakeca23ee22002-07-02 20:37:12 +0000125\begin{memberdesc}{initial_indent}
126(default: \code{''}) String that will be prepended to the first line
127of wrapped output. Counts towards the length of the first line.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000128\end{memberdesc}
129
Fred Drakeca23ee22002-07-02 20:37:12 +0000130\begin{memberdesc}{subsequent_indent}
131(default: \code{''}) String that will be prepended to all lines of
132wrapped output except the first. Counts towards the length of each
133line except the first.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000134\end{memberdesc}
135
Fred Drakeca23ee22002-07-02 20:37:12 +0000136\begin{memberdesc}{fix_sentence_endings}
137(default: \code{False}) If true, \class{TextWrapper} attempts to detect
Greg Wardae64f3ad2002-06-29 02:38:50 +0000138sentence endings and ensure that sentences are always separated by
139exactly two spaces. This is generally desired for text in a monospaced
140font. However, the sentence detection algorithm is imperfect: it
141assumes that a sentence ending consists of a lowercase letter followed
142by one of \character{.},
143\character{!}, or \character{?}, possibly followed by one of
Greg Ward285f4a72002-07-02 21:48:12 +0000144\character{"} or \character{'}, followed by a space. One problem
145with this is algorithm is that it is unable to detect the difference
146between ``Dr.'' in
Fred Drake228f6e42002-07-03 05:08:48 +0000147
Greg Wardae64f3ad2002-06-29 02:38:50 +0000148\begin{verbatim}
149[...] Dr. Frankenstein's monster [...]
150\end{verbatim}
Fred Drake228f6e42002-07-03 05:08:48 +0000151
Greg Wardae64f3ad2002-06-29 02:38:50 +0000152and ``Spot.'' in
Fred Drake228f6e42002-07-03 05:08:48 +0000153
Greg Wardae64f3ad2002-06-29 02:38:50 +0000154\begin{verbatim}
Greg Ward285f4a72002-07-02 21:48:12 +0000155[...] See Spot. See Spot run [...]
Greg Wardae64f3ad2002-06-29 02:38:50 +0000156\end{verbatim}
Fred Drake228f6e42002-07-03 05:08:48 +0000157
158\member{fix_sentence_endings} is false by default.
159
160Since the sentence detection algorithm relies on
161\code{string.lowercase} for the definition of ``lowercase letter,''
162and a convention of using two spaces after a period to separate
163sentences on the same line, it is specific to English-language texts.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000164\end{memberdesc}
165
Fred Drakeca23ee22002-07-02 20:37:12 +0000166\begin{memberdesc}{break_long_words}
Greg Ward285f4a72002-07-02 21:48:12 +0000167(default: \code{True}) If true, then words longer than
Fred Drake228f6e42002-07-03 05:08:48 +0000168\member{width} will be broken in order to ensure that no lines are
169longer than \member{width}. If it is false, long words will not be
170broken, and some lines may be longer than \member{width}. (Long words
171will be put on a line by themselves, in order to minimize the amount
172by which \member{width} is exceeded.)
Greg Wardae64f3ad2002-06-29 02:38:50 +0000173\end{memberdesc}
174
175\class{TextWrapper} also provides two public methods, analogous to the
176module-level convenience functions:
177
178\begin{methoddesc}{wrap}{text}
Fred Drake228f6e42002-07-03 05:08:48 +0000179Wraps the single paragraph in \var{text} (a string) so every line is
180at most \member{width} characters long. All wrapping options are
181taken from instance attributes of the \class{TextWrapper} instance.
182Returns a list of output lines, without final newlines.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000183\end{methoddesc}
184
185\begin{methoddesc}{fill}{text}
186Wraps the single paragraph in \var{text}, and returns a single string
187containing the wrapped paragraph.
188\end{methoddesc}