blob: e319333e75897a46fa4c1a6575b5599ed88036b7 [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}
Raymond Hettinger4c9800d2004-02-08 18:09:32 +000050Remove any whitespace that can be uniformly removed from the left
Greg Ward8f6329c2003-05-08 02:09:49 +000051of every line in \var{text}.
52
53This is typically used to make triple-quoted strings line up with
54the left edge of screen/whatever, while still presenting it in the
55source code in indented form.
56
57For example:
58\begin{verbatim}
59def test():
60 # end first line with \ to avoid the empty line!
61 s = '''\
Greg Ward86e17902003-05-08 02:12:35 +000062 hello
63 world
Greg Ward8f6329c2003-05-08 02:09:49 +000064 '''
Greg Ward86e17902003-05-08 02:12:35 +000065 print repr(s) # prints ' hello\n world\n '
66 print repr(dedent(s)) # prints 'hello\n world\n'
Greg Ward8f6329c2003-05-08 02:09:49 +000067\end{verbatim}
68\end{funcdesc}
69
Greg Ward285f4a72002-07-02 21:48:12 +000070\begin{classdesc}{TextWrapper}{...}
71The \class{TextWrapper} constructor accepts a number of optional
72keyword arguments. Each argument corresponds to one instance attribute,
73so for example
Greg Wardae64f3ad2002-06-29 02:38:50 +000074\begin{verbatim}
75wrapper = TextWrapper(initial_indent="* ")
76\end{verbatim}
77is the same as
78\begin{verbatim}
79wrapper = TextWrapper()
80wrapper.initial_indent = "* "
81\end{verbatim}
82
83You can re-use the same \class{TextWrapper} object many times, and you
84can change any of its options through direct assignment to instance
Fred Drakeca23ee22002-07-02 20:37:12 +000085attributes between uses.
86\end{classdesc}
Greg Wardae64f3ad2002-06-29 02:38:50 +000087
Greg Ward285f4a72002-07-02 21:48:12 +000088The \class{TextWrapper} instance attributes (and keyword arguments to
89the constructor) are as follows:
Fred Drakeca23ee22002-07-02 20:37:12 +000090
Greg Ward285f4a72002-07-02 21:48:12 +000091\begin{memberdesc}{width}
Fred Drake228f6e42002-07-03 05:08:48 +000092(default: \code{70}) The maximum length of wrapped lines. As long as
93there are no individual words in the input text longer than
94\member{width}, \class{TextWrapper} guarantees that no output line
95will be longer than \member{width} characters.
Greg Ward285f4a72002-07-02 21:48:12 +000096\end{memberdesc}
Fred Drakeca23ee22002-07-02 20:37:12 +000097
98\begin{memberdesc}{expand_tabs}
Greg Ward285f4a72002-07-02 21:48:12 +000099(default: \code{True}) If true, then all tab characters in \var{text}
100will be expanded to spaces using the \method{expand_tabs()} method of
Greg Wardae64f3ad2002-06-29 02:38:50 +0000101\var{text}.
102\end{memberdesc}
103
Fred Drakeca23ee22002-07-02 20:37:12 +0000104\begin{memberdesc}{replace_whitespace}
Fred Drake228f6e42002-07-03 05:08:48 +0000105(default: \code{True}) If true, each whitespace character (as defined
106by \code{string.whitespace}) remaining after tab expansion will be
107replaced by a single space. \note{If \member{expand_tabs} is false
108and \member{replace_whitespace} is true, each tab character will be
109replaced by a single space, which is \emph{not} the same as tab
110expansion.}
Greg Wardae64f3ad2002-06-29 02:38:50 +0000111\end{memberdesc}
112
Fred Drakeca23ee22002-07-02 20:37:12 +0000113\begin{memberdesc}{initial_indent}
114(default: \code{''}) String that will be prepended to the first line
115of wrapped output. Counts towards the length of the first line.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000116\end{memberdesc}
117
Fred Drakeca23ee22002-07-02 20:37:12 +0000118\begin{memberdesc}{subsequent_indent}
119(default: \code{''}) String that will be prepended to all lines of
120wrapped output except the first. Counts towards the length of each
121line except the first.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000122\end{memberdesc}
123
Fred Drakeca23ee22002-07-02 20:37:12 +0000124\begin{memberdesc}{fix_sentence_endings}
125(default: \code{False}) If true, \class{TextWrapper} attempts to detect
Greg Wardae64f3ad2002-06-29 02:38:50 +0000126sentence endings and ensure that sentences are always separated by
127exactly two spaces. This is generally desired for text in a monospaced
128font. However, the sentence detection algorithm is imperfect: it
129assumes that a sentence ending consists of a lowercase letter followed
130by one of \character{.},
131\character{!}, or \character{?}, possibly followed by one of
Greg Ward285f4a72002-07-02 21:48:12 +0000132\character{"} or \character{'}, followed by a space. One problem
133with this is algorithm is that it is unable to detect the difference
134between ``Dr.'' in
Fred Drake228f6e42002-07-03 05:08:48 +0000135
Greg Wardae64f3ad2002-06-29 02:38:50 +0000136\begin{verbatim}
137[...] Dr. Frankenstein's monster [...]
138\end{verbatim}
Fred Drake228f6e42002-07-03 05:08:48 +0000139
Greg Wardae64f3ad2002-06-29 02:38:50 +0000140and ``Spot.'' in
Fred Drake228f6e42002-07-03 05:08:48 +0000141
Greg Wardae64f3ad2002-06-29 02:38:50 +0000142\begin{verbatim}
Greg Ward285f4a72002-07-02 21:48:12 +0000143[...] See Spot. See Spot run [...]
Greg Wardae64f3ad2002-06-29 02:38:50 +0000144\end{verbatim}
Fred Drake228f6e42002-07-03 05:08:48 +0000145
146\member{fix_sentence_endings} is false by default.
147
148Since the sentence detection algorithm relies on
149\code{string.lowercase} for the definition of ``lowercase letter,''
150and a convention of using two spaces after a period to separate
151sentences on the same line, it is specific to English-language texts.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000152\end{memberdesc}
153
Fred Drakeca23ee22002-07-02 20:37:12 +0000154\begin{memberdesc}{break_long_words}
Greg Ward285f4a72002-07-02 21:48:12 +0000155(default: \code{True}) If true, then words longer than
Fred Drake228f6e42002-07-03 05:08:48 +0000156\member{width} will be broken in order to ensure that no lines are
157longer than \member{width}. If it is false, long words will not be
158broken, and some lines may be longer than \member{width}. (Long words
159will be put on a line by themselves, in order to minimize the amount
160by which \member{width} is exceeded.)
Greg Wardae64f3ad2002-06-29 02:38:50 +0000161\end{memberdesc}
162
163\class{TextWrapper} also provides two public methods, analogous to the
164module-level convenience functions:
165
166\begin{methoddesc}{wrap}{text}
Fred Drake228f6e42002-07-03 05:08:48 +0000167Wraps the single paragraph in \var{text} (a string) so every line is
168at most \member{width} characters long. All wrapping options are
169taken from instance attributes of the \class{TextWrapper} instance.
170Returns a list of output lines, without final newlines.
Greg Wardae64f3ad2002-06-29 02:38:50 +0000171\end{methoddesc}
172
173\begin{methoddesc}{fill}{text}
174Wraps the single paragraph in \var{text}, and returns a single string
175containing the wrapped paragraph.
176\end{methoddesc}