<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"module-codecs.html" />
<link rel=
"prev" href=
"module-cStringIO.html" />
<link rel=
"parent" href=
"strings.html" />
<link rel=
"next" href=
"module-codecs.html" />
<meta name='aesop' content='information'
/>
<title>4.8 textwrap -- Text wrapping and filling
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"4.7 cStringIO "
href=
"module-cStringIO.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"4. String Services"
href=
"strings.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"4.9 codecs "
href=
"module-codecs.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-cStringIO.html">4.7 cStringIO
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"strings.html">4. String Services
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-codecs.html">4.9 codecs
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION006800000000000000000">
4.8 <tt class=
"module">textwrap
</tt> --
Text wrapping and filling
</A>
<A NAME=
"module-textwrap"></A>
<span class=
"versionnote">New in version
2.3.
</span>
The
<tt class=
"module">textwrap
</tt> module provides two convenience functions,
<tt class=
"function">wrap()
</tt> and
<tt class=
"function">fill()
</tt>, as well as
<tt class=
"class">TextWrapper
</tt>, the class that does all the work, and a utility function
<tt class=
"function">dedent()
</tt>. If you're just wrapping or filling one or two
text strings, the convenience functions should be good enough; otherwise,
you should use an instance of
<tt class=
"class">TextWrapper
</tt> for efficiency.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
961' xml:id='l2h-
961'
class=
"function">wrap
</tt></b>(
</nobr></td>
<td><var>text
</var><big>[
</big><var>, width
</var><big>[
</big><var>, ...
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Wraps the single paragraph in
<var>text
</var> (a string) so every line is at
most
<var>width
</var> characters long. Returns a list of output lines,
Optional keyword arguments correspond to the instance attributes of
<tt class=
"class">TextWrapper
</tt>, documented below.
<var>width
</var> defaults to
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
962' xml:id='l2h-
962'
class=
"function">fill
</tt></b>(
</nobr></td>
<td><var>text
</var><big>[
</big><var>, width
</var><big>[
</big><var>, ...
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Wraps the single paragraph in
<var>text
</var>, and returns a single string
containing the wrapped paragraph.
<tt class=
"function">fill()
</tt> is shorthand for
<div class=
"verbatim"><pre>
"\n".join(wrap(text, ...))
In particular,
<tt class=
"function">fill()
</tt> accepts exactly the same keyword
arguments as
<tt class=
"function">wrap()
</tt>.
Both
<tt class=
"function">wrap()
</tt> and
<tt class=
"function">fill()
</tt> work by creating a
<tt class=
"class">TextWrapper
</tt> instance and calling a single method on it. That
instance is not reused, so for applications that wrap/fill many text
strings, it will be more efficient for you to create your own
<tt class=
"class">TextWrapper
</tt> object.
An additional utility function,
<tt class=
"function">dedent()
</tt>, is provided to
remove indentation from strings that have unwanted whitespace to the
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
963' xml:id='l2h-
963'
class=
"function">dedent
</tt></b>(
</nobr></td>
<td><var>text
</var>)
</td></tr></table></dt>
Remove any whitespace that can be uniformly removed from the left
of every line in
<var>text
</var>.
This is typically used to make triple-quoted strings line up with
the left edge of screen/whatever, while still presenting it in the
source code in indented form.
<div class=
"verbatim"><pre>
# end first line with \ to avoid the empty line!
print repr(s) # prints ' hello\n world\n '
print repr(dedent(s)) # prints 'hello\n world\n'
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
964' xml:id='l2h-
964'
class=
"class">TextWrapper
</tt></b>(
</nobr></td>
<td><var>...
</var>)
</td></tr></table></dt>
The
<tt class=
"class">TextWrapper
</tt> constructor accepts a number of optional
keyword arguments. Each argument corresponds to one instance attribute,
<div class=
"verbatim"><pre>
wrapper = TextWrapper(
initial_indent=
"* ")
<div class=
"verbatim"><pre>
wrapper.initial_indent =
"* "
You can re-use the same
<tt class=
"class">TextWrapper
</tt> object many times, and you
can change any of its options through direct assignment to instance
The
<tt class=
"class">TextWrapper
</tt> instance attributes (and keyword arguments to
the constructor) are as follows:
<dl><dt><b><tt id='l2h-
965' xml:id='l2h-
965'
class=
"member">width
</tt></b></dt>
(default:
<code>70</code>) The maximum length of wrapped lines. As long as
there are no individual words in the input text longer than
<tt class=
"member">width
</tt>,
<tt class=
"class">TextWrapper
</tt> guarantees that no output line
will be longer than
<tt class=
"member">width
</tt> characters.
<dl><dt><b><tt id='l2h-
966' xml:id='l2h-
966'
class=
"member">expand_tabs
</tt></b></dt>
(default:
<code>True
</code>) If true, then all tab characters in
<var>text
</var>
will be expanded to spaces using the
<tt class=
"method">expandtabs()
</tt> method of
<dl><dt><b><tt id='l2h-
967' xml:id='l2h-
967'
class=
"member">replace_whitespace
</tt></b></dt>
(default:
<code>True
</code>) If true, each whitespace character (as defined
by
<code>string.whitespace
</code>) remaining after tab expansion will be
replaced by a single space.
<span class=
"note"><b class=
"label">Note:
</b>
If
<tt class=
"member">expand_tabs
</tt> is false
and
<tt class=
"member">replace_whitespace
</tt> is true, each tab character will be
replaced by a single space, which is
<em>not
</em> the same as tab
<dl><dt><b><tt id='l2h-
968' xml:id='l2h-
968'
class=
"member">initial_indent
</tt></b></dt>
(default:
<code>''
</code>) String that will be prepended to the first line
of wrapped output. Counts towards the length of the first line.
<dl><dt><b><tt id='l2h-
969' xml:id='l2h-
969'
class=
"member">subsequent_indent
</tt></b></dt>
(default:
<code>''
</code>) String that will be prepended to all lines of
wrapped output except the first. Counts towards the length of each
<dl><dt><b><tt id='l2h-
970' xml:id='l2h-
970'
class=
"member">fix_sentence_endings
</tt></b></dt>
(default:
<code>False
</code>) If true,
<tt class=
"class">TextWrapper
</tt> attempts to detect
sentence endings and ensure that sentences are always separated by
exactly two spaces. This is generally desired for text in a monospaced
font. However, the sentence detection algorithm is imperfect: it
assumes that a sentence ending consists of a lowercase letter followed
by one of
"<tt class="character
">.</tt>",
"<tt class="character
">!</tt>", or
"<tt class="character
">?</tt>", possibly followed by one of
"<tt class="character
">"</tt>" or "<tt class=
"character">'
</tt>", followed by a space. One problem
with this is algorithm is that it is unable to detect the difference
<div class="verbatim
"><pre>
[...] Dr. Frankenstein's monster [...]
<div class="verbatim
"><pre>
[...] See Spot. See Spot run [...]
<tt class="member
">fix_sentence_endings</tt> is false by default.
Since the sentence detection algorithm relies on
<code>string.lowercase</code> for the definition of ``lowercase letter,''
and a convention of using two spaces after a period to separate
sentences on the same line, it is specific to English-language texts.
<dl><dt><b><tt id='l2h-971' xml:id='l2h-971' class="member
">break_long_words</tt></b></dt>
(default: <code>True</code>) If true, then words longer than
<tt class="member
">width</tt> will be broken in order to ensure that no lines are
longer than <tt class="member
">width</tt>. If it is false, long words will not be
broken, and some lines may be longer than <tt class="member
">width</tt>. (Long words
will be put on a line by themselves, in order to minimize the amount
by which <tt class="member
">width</tt> is exceeded.)
<tt class="class
">TextWrapper</tt> also provides two public methods, analogous to the
module-level convenience functions:
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline
">
<td><nobr><b><tt id='l2h-972' xml:id='l2h-972' class="method
">wrap</tt></b>(</nobr></td>
<td><var>text</var>)</td></tr></table></dt>
Wraps the single paragraph in <var>text</var> (a string) so every line is
at most <tt class="member
">width</tt> characters long. All wrapping options are
taken from instance attributes of the <tt class="class
">TextWrapper</tt> instance.
Returns a list of output lines, without final newlines.
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline
">
<td><nobr><b><tt id='l2h-973' xml:id='l2h-973' class="method
">fill</tt></b>(</nobr></td>
<td><var>text</var>)</td></tr></table></dt>
Wraps the single paragraph in <var>text</var>, and returns a single string
containing the wrapped paragraph.
<div class='online-navigation'>
<table align="center
" width="100%
" cellpadding="0" cellspacing="2">
<td class='online-navigation'><a rel="prev
" title="4.7 cStringIO
"
href="module-cStringIO.html
"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent
" title="4. String Services
"
href="strings.html
"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next
" title="4.9 codecs
"
href="module-codecs.html
"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center
" width="100%
">Python Library Reference</td>
<td class='online-navigation'><a rel="contents
" title="Table of Contents
"
href="contents.html
"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html
" title="Module Index
"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index
" title="Index
"
href="genindex.html
"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
<div class='online-navigation'>
<b class="navlabel
">Previous:</b>
<a class="sectref
" rel="prev
" href="module-cStringIO.html
">4.7 cStringIO </A>
<b class="navlabel
">Up:</b>
<a class="sectref
" rel="parent
" href="strings.html
">4. String Services</A>
<b class="navlabel
">Next:</b>
<a class="sectref
" rel="next
" href="module-codecs.html
">4.9 codecs </A>
<span class="release-info
">Release 2.4.2, documentation updated on 28 September 2005.</span>
<!--End of Navigation Panel-->
See <i><a href="about.html
">About this document...</a></i> for information on suggesting changes.