~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/library/textwrap.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>7.7. textwrap — Text wrapping and filling &mdash; Python 2.7.11 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '2.7.11',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 2.7.11 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
 
    <link rel="up" title="7. String Services" href="strings.html" />
34
 
    <link rel="next" title="7.8. codecs — Codec registry and base classes" href="codecs.html" />
35
 
    <link rel="prev" title="7.5. StringIO — Read and write strings as files" href="stringio.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
 
40
 
    
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="codecs.html" title="7.8. codecs — Codec registry and base classes"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="stringio.html" title="7.5. StringIO — Read and write strings as files"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">2.7.11</span>
64
 
          <a href="../index.html">Documentation</a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="strings.html" accesskey="U">7. String Services</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
77
 
  <div class="section" id="module-textwrap">
78
 
<span id="textwrap-text-wrapping-and-filling"></span><h1>7.7. <a class="reference internal" href="#module-textwrap" title="textwrap: Text wrapping and filling"><code class="xref py py-mod docutils literal"><span class="pre">textwrap</span></code></a> &#8212; Text wrapping and filling<a class="headerlink" href="#module-textwrap" title="Permalink to this headline">¶</a></h1>
79
 
<div class="versionadded">
80
 
<p><span class="versionmodified">New in version 2.3.</span></p>
81
 
</div>
82
 
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/textwrap.py">Lib/textwrap.py</a></p>
83
 
<hr class="docutils" />
84
 
<p>The <a class="reference internal" href="#module-textwrap" title="textwrap: Text wrapping and filling"><code class="xref py py-mod docutils literal"><span class="pre">textwrap</span></code></a> module provides two convenience functions, <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a> and
85
 
<a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a>, as well as <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>, the class that does all the work,
86
 
and a utility function  <a class="reference internal" href="#textwrap.dedent" title="textwrap.dedent"><code class="xref py py-func docutils literal"><span class="pre">dedent()</span></code></a>.  If you&#8217;re just wrapping or filling one
87
 
or two  text strings, the convenience functions should be good enough;
88
 
otherwise,  you should use an instance of <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> for efficiency.</p>
89
 
<dl class="function">
90
 
<dt id="textwrap.wrap">
91
 
<code class="descclassname">textwrap.</code><code class="descname">wrap</code><span class="sig-paren">(</span><em>text</em><span class="optional">[</span>, <em>width</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.wrap" title="Permalink to this definition">¶</a></dt>
92
 
<dd><p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most <em>width</em>
93
 
characters long.  Returns a list of output lines, without final newlines.</p>
94
 
<p>Optional keyword arguments correspond to the instance attributes of
95
 
<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>, documented below.  <em>width</em> defaults to <code class="docutils literal"><span class="pre">70</span></code>.</p>
96
 
<p>See the <a class="reference internal" href="#textwrap.TextWrapper.wrap" title="textwrap.TextWrapper.wrap"><code class="xref py py-meth docutils literal"><span class="pre">TextWrapper.wrap()</span></code></a> method for additional details on how
97
 
<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a> behaves.</p>
98
 
</dd></dl>
99
 
 
100
 
<dl class="function">
101
 
<dt id="textwrap.fill">
102
 
<code class="descclassname">textwrap.</code><code class="descname">fill</code><span class="sig-paren">(</span><em>text</em><span class="optional">[</span>, <em>width</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.fill" title="Permalink to this definition">¶</a></dt>
103
 
<dd><p>Wraps the single paragraph in <em>text</em>, and returns a single string containing the
104
 
wrapped paragraph.  <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a> is shorthand for</p>
105
 
<div class="highlight-python"><div class="highlight"><pre><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span>
106
 
</pre></div>
107
 
</div>
108
 
<p>In particular, <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a> accepts exactly the same keyword arguments as
109
 
<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a>.</p>
110
 
</dd></dl>
111
 
 
112
 
<p>Both <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code></a> and <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal"><span class="pre">fill()</span></code></a> work by creating a <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a>
113
 
instance and calling a single method on it.  That instance is not reused, so for
114
 
applications that wrap/fill many text strings, it will be more efficient for you
115
 
to create your own <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> object.</p>
116
 
<p>Text is preferably wrapped on whitespaces and right after the hyphens in
117
 
hyphenated words; only then will long words be broken if necessary, unless
118
 
<a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><code class="xref py py-attr docutils literal"><span class="pre">TextWrapper.break_long_words</span></code></a> is set to false.</p>
119
 
<p>An additional utility function, <a class="reference internal" href="#textwrap.dedent" title="textwrap.dedent"><code class="xref py py-func docutils literal"><span class="pre">dedent()</span></code></a>, is provided to remove
120
 
indentation from strings that have unwanted whitespace to the left of the text.</p>
121
 
<dl class="function">
122
 
<dt id="textwrap.dedent">
123
 
<code class="descclassname">textwrap.</code><code class="descname">dedent</code><span class="sig-paren">(</span><em>text</em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.dedent" title="Permalink to this definition">¶</a></dt>
124
 
<dd><p>Remove any common leading whitespace from every line in <em>text</em>.</p>
125
 
<p>This can be used to make triple-quoted strings line up with the left edge of the
126
 
display, while still presenting them in the source code in indented form.</p>
127
 
<p>Note that tabs and spaces are both treated as whitespace, but they are not
128
 
equal: the lines <code class="docutils literal"><span class="pre">&quot;</span>&nbsp; <span class="pre">hello&quot;</span></code> and <code class="docutils literal"><span class="pre">&quot;\thello&quot;</span></code> are considered to have no
129
 
common leading whitespace.  (This behaviour is new in Python 2.5; older versions
130
 
of this module incorrectly expanded tabs before searching for common leading
131
 
whitespace.)</p>
132
 
<p>For example:</p>
133
 
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
134
 
    <span class="c1"># end first line with \ to avoid the empty line!</span>
135
 
    <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;</span><span class="se">\</span>
136
 
<span class="s1">    hello</span>
137
 
<span class="s1">      world</span>
138
 
<span class="s1">    &#39;&#39;&#39;</span>
139
 
    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>          <span class="c1"># prints &#39;    hello\n      world\n    &#39;</span>
140
 
    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">dedent</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>  <span class="c1"># prints &#39;hello\n  world\n&#39;</span>
141
 
</pre></div>
142
 
</div>
143
 
</dd></dl>
144
 
 
145
 
<dl class="class">
146
 
<dt id="textwrap.TextWrapper">
147
 
<em class="property">class </em><code class="descclassname">textwrap.</code><code class="descname">TextWrapper</code><span class="sig-paren">(</span><em>...</em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.TextWrapper" title="Permalink to this definition">¶</a></dt>
148
 
<dd><p>The <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> constructor accepts a number of optional keyword
149
 
arguments.  Each argument corresponds to one instance attribute, so for example</p>
150
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">(</span><span class="n">initial_indent</span><span class="o">=</span><span class="s2">&quot;* &quot;</span><span class="p">)</span>
151
 
</pre></div>
152
 
</div>
153
 
<p>is the same as</p>
154
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">()</span>
155
 
<span class="n">wrapper</span><span class="o">.</span><span class="n">initial_indent</span> <span class="o">=</span> <span class="s2">&quot;* &quot;</span>
156
 
</pre></div>
157
 
</div>
158
 
<p>You can re-use the same <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> object many times, and you can
159
 
change any of its options through direct assignment to instance attributes
160
 
between uses.</p>
161
 
<p>The <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> instance attributes (and keyword arguments to the
162
 
constructor) are as follows:</p>
163
 
<dl class="attribute">
164
 
<dt id="textwrap.TextWrapper.width">
165
 
<code class="descname">width</code><a class="headerlink" href="#textwrap.TextWrapper.width" title="Permalink to this definition">¶</a></dt>
166
 
<dd><p>(default: <code class="docutils literal"><span class="pre">70</span></code>) The maximum length of wrapped lines.  As long as there
167
 
are no individual words in the input text longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>,
168
 
<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> guarantees that no output line will be longer than
169
 
<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a> characters.</p>
170
 
</dd></dl>
171
 
 
172
 
<dl class="attribute">
173
 
<dt id="textwrap.TextWrapper.expand_tabs">
174
 
<code class="descname">expand_tabs</code><a class="headerlink" href="#textwrap.TextWrapper.expand_tabs" title="Permalink to this definition">¶</a></dt>
175
 
<dd><p>(default: <code class="docutils literal"><span class="pre">True</span></code>) If true, then all tab characters in <em>text</em> will be
176
 
expanded to spaces using the <code class="xref py py-meth docutils literal"><span class="pre">expandtabs()</span></code> method of <em>text</em>.</p>
177
 
</dd></dl>
178
 
 
179
 
<dl class="attribute">
180
 
<dt id="textwrap.TextWrapper.replace_whitespace">
181
 
<code class="descname">replace_whitespace</code><a class="headerlink" href="#textwrap.TextWrapper.replace_whitespace" title="Permalink to this definition">¶</a></dt>
182
 
<dd><p>(default: <code class="docutils literal"><span class="pre">True</span></code>) If true, after tab expansion but before wrapping,
183
 
the <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-meth docutils literal"><span class="pre">wrap()</span></code></a> method will replace each whitespace character
184
 
with a single space.  The whitespace characters replaced are
185
 
as follows: tab, newline, vertical tab, formfeed, and carriage
186
 
return (<code class="docutils literal"><span class="pre">'\t\n\v\f\r'</span></code>).</p>
187
 
<div class="admonition note">
188
 
<p class="first admonition-title">Note</p>
189
 
<p class="last">If <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code class="xref py py-attr docutils literal"><span class="pre">expand_tabs</span></code></a> is false and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></code></a> is true,
190
 
each tab character will be replaced by a single space, which is <em>not</em>
191
 
the same as tab expansion.</p>
192
 
</div>
193
 
<div class="admonition note">
194
 
<p class="first admonition-title">Note</p>
195
 
<p class="last">If <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></code></a> is false, newlines may appear in the
196
 
middle of a line and cause strange output. For this reason, text should
197
 
be split into paragraphs (using <a class="reference internal" href="stdtypes.html#str.splitlines" title="str.splitlines"><code class="xref py py-meth docutils literal"><span class="pre">str.splitlines()</span></code></a> or similar)
198
 
which are wrapped separately.</p>
199
 
</div>
200
 
</dd></dl>
201
 
 
202
 
<dl class="attribute">
203
 
<dt id="textwrap.TextWrapper.drop_whitespace">
204
 
<code class="descname">drop_whitespace</code><a class="headerlink" href="#textwrap.TextWrapper.drop_whitespace" title="Permalink to this definition">¶</a></dt>
205
 
<dd><p>(default: <code class="docutils literal"><span class="pre">True</span></code>) If true, whitespace at the beginning and ending of
206
 
every line (after wrapping but before indenting) is dropped.
207
 
Whitespace at the beginning of the paragraph, however, is not dropped
208
 
if non-whitespace follows it.  If whitespace being dropped takes up an
209
 
entire line, the whole line is dropped.</p>
210
 
<div class="versionadded">
211
 
<p><span class="versionmodified">New in version 2.6: </span>Whitespace was always dropped in earlier versions.</p>
212
 
</div>
213
 
</dd></dl>
214
 
 
215
 
<dl class="attribute">
216
 
<dt id="textwrap.TextWrapper.initial_indent">
217
 
<code class="descname">initial_indent</code><a class="headerlink" href="#textwrap.TextWrapper.initial_indent" title="Permalink to this definition">¶</a></dt>
218
 
<dd><p>(default: <code class="docutils literal"><span class="pre">''</span></code>) String that will be prepended to the first line of
219
 
wrapped output.  Counts towards the length of the first line.  The empty
220
 
string is not indented.</p>
221
 
</dd></dl>
222
 
 
223
 
<dl class="attribute">
224
 
<dt id="textwrap.TextWrapper.subsequent_indent">
225
 
<code class="descname">subsequent_indent</code><a class="headerlink" href="#textwrap.TextWrapper.subsequent_indent" title="Permalink to this definition">¶</a></dt>
226
 
<dd><p>(default: <code class="docutils literal"><span class="pre">''</span></code>) String that will be prepended to all lines of wrapped
227
 
output except the first.  Counts towards the length of each line except
228
 
the first.</p>
229
 
</dd></dl>
230
 
 
231
 
<dl class="attribute">
232
 
<dt id="textwrap.TextWrapper.fix_sentence_endings">
233
 
<code class="descname">fix_sentence_endings</code><a class="headerlink" href="#textwrap.TextWrapper.fix_sentence_endings" title="Permalink to this definition">¶</a></dt>
234
 
<dd><p>(default: <code class="docutils literal"><span class="pre">False</span></code>) If true, <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> attempts to detect
235
 
sentence endings and ensure that sentences are always separated by exactly
236
 
two spaces.  This is generally desired for text in a monospaced font.
237
 
However, the sentence detection algorithm is imperfect: it assumes that a
238
 
sentence ending consists of a lowercase letter followed by one of <code class="docutils literal"><span class="pre">'.'</span></code>,
239
 
<code class="docutils literal"><span class="pre">'!'</span></code>, or <code class="docutils literal"><span class="pre">'?'</span></code>, possibly followed by one of <code class="docutils literal"><span class="pre">'&quot;'</span></code> or <code class="docutils literal"><span class="pre">&quot;'&quot;</span></code>,
240
 
followed by a space.  One problem with this is algorithm is that it is
241
 
unable to detect the difference between &#8220;Dr.&#8221; in</p>
242
 
<div class="highlight-python"><div class="highlight"><pre>[...] Dr. Frankenstein&#39;s monster [...]
243
 
</pre></div>
244
 
</div>
245
 
<p>and &#8220;Spot.&#8221; in</p>
246
 
<div class="highlight-python"><div class="highlight"><pre>[...] See Spot. See Spot run [...]
247
 
</pre></div>
248
 
</div>
249
 
<p><a class="reference internal" href="#textwrap.TextWrapper.fix_sentence_endings" title="textwrap.TextWrapper.fix_sentence_endings"><code class="xref py py-attr docutils literal"><span class="pre">fix_sentence_endings</span></code></a> is false by default.</p>
250
 
<p>Since the sentence detection algorithm relies on <code class="docutils literal"><span class="pre">string.lowercase</span></code> for
251
 
the definition of &#8220;lowercase letter,&#8221; and a convention of using two spaces
252
 
after a period to separate sentences on the same line, it is specific to
253
 
English-language texts.</p>
254
 
</dd></dl>
255
 
 
256
 
<dl class="attribute">
257
 
<dt id="textwrap.TextWrapper.break_long_words">
258
 
<code class="descname">break_long_words</code><a class="headerlink" href="#textwrap.TextWrapper.break_long_words" title="Permalink to this definition">¶</a></dt>
259
 
<dd><p>(default: <code class="docutils literal"><span class="pre">True</span></code>) If true, then words longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a> will be
260
 
broken in order to ensure that no lines are longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>.  If
261
 
it is false, long words will not be broken, and some lines may be longer
262
 
than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a>.  (Long words will be put on a line by themselves, in
263
 
order to minimize the amount by which <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a> is exceeded.)</p>
264
 
</dd></dl>
265
 
 
266
 
<dl class="attribute">
267
 
<dt id="textwrap.TextWrapper.break_on_hyphens">
268
 
<code class="descname">break_on_hyphens</code><a class="headerlink" href="#textwrap.TextWrapper.break_on_hyphens" title="Permalink to this definition">¶</a></dt>
269
 
<dd><p>(default: <code class="docutils literal"><span class="pre">True</span></code>) If true, wrapping will occur preferably on whitespaces
270
 
and right after hyphens in compound words, as it is customary in English.
271
 
If false, only whitespaces will be considered as potentially good places
272
 
for line breaks, but you need to set <a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><code class="xref py py-attr docutils literal"><span class="pre">break_long_words</span></code></a> to false if
273
 
you want truly insecable words.  Default behaviour in previous versions
274
 
was to always allow breaking hyphenated words.</p>
275
 
<div class="versionadded">
276
 
<p><span class="versionmodified">New in version 2.6.</span></p>
277
 
</div>
278
 
</dd></dl>
279
 
 
280
 
<p><a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> also provides two public methods, analogous to the
281
 
module-level convenience functions:</p>
282
 
<dl class="method">
283
 
<dt id="textwrap.TextWrapper.wrap">
284
 
<code class="descname">wrap</code><span class="sig-paren">(</span><em>text</em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.TextWrapper.wrap" title="Permalink to this definition">¶</a></dt>
285
 
<dd><p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most
286
 
<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal"><span class="pre">width</span></code></a> characters long.  All wrapping options are taken from
287
 
instance attributes of the <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code></a> instance.  Returns a list
288
 
of output lines, without final newlines.  If the wrapped output has no
289
 
content, the returned list is empty.</p>
290
 
</dd></dl>
291
 
 
292
 
<dl class="method">
293
 
<dt id="textwrap.TextWrapper.fill">
294
 
<code class="descname">fill</code><span class="sig-paren">(</span><em>text</em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.TextWrapper.fill" title="Permalink to this definition">¶</a></dt>
295
 
<dd><p>Wraps the single paragraph in <em>text</em>, and returns a single string
296
 
containing the wrapped paragraph.</p>
297
 
</dd></dl>
298
 
 
299
 
</dd></dl>
300
 
 
301
 
</div>
302
 
 
303
 
 
304
 
          </div>
305
 
        </div>
306
 
      </div>
307
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
308
 
        <div class="sphinxsidebarwrapper">
309
 
  <h4>Previous topic</h4>
310
 
  <p class="topless"><a href="stringio.html"
311
 
                        title="previous chapter">7.5. <code class="docutils literal"><span class="pre">StringIO</span></code> &#8212; Read and write strings as files</a></p>
312
 
  <h4>Next topic</h4>
313
 
  <p class="topless"><a href="codecs.html"
314
 
                        title="next chapter">7.8. <code class="docutils literal"><span class="pre">codecs</span></code> &#8212; Codec registry and base classes</a></p>
315
 
<h3>This Page</h3>
316
 
<ul class="this-page-menu">
317
 
  <li><a href="../bugs.html">Report a Bug</a></li>
318
 
  <li><a href="../_sources/library/textwrap.txt"
319
 
         rel="nofollow">Show Source</a></li>
320
 
</ul>
321
 
 
322
 
<div id="searchbox" style="display: none" role="search">
323
 
  <h3>Quick search</h3>
324
 
    <form class="search" action="../search.html" method="get">
325
 
      <input type="text" name="q" />
326
 
      <input type="submit" value="Go" />
327
 
      <input type="hidden" name="check_keywords" value="yes" />
328
 
      <input type="hidden" name="area" value="default" />
329
 
    </form>
330
 
    <p class="searchtip" style="font-size: 90%">
331
 
    Enter search terms or a module, class or function name.
332
 
    </p>
333
 
</div>
334
 
<script type="text/javascript">$('#searchbox').show(0);</script>
335
 
        </div>
336
 
      </div>
337
 
      <div class="clearer"></div>
338
 
    </div>  
339
 
    <div class="related" role="navigation" aria-label="related navigation">
340
 
      <h3>Navigation</h3>
341
 
      <ul>
342
 
        <li class="right" style="margin-right: 10px">
343
 
          <a href="../genindex.html" title="General Index"
344
 
             >index</a></li>
345
 
        <li class="right" >
346
 
          <a href="../py-modindex.html" title="Python Module Index"
347
 
             >modules</a> |</li>
348
 
        <li class="right" >
349
 
          <a href="codecs.html" title="7.8. codecs — Codec registry and base classes"
350
 
             >next</a> |</li>
351
 
        <li class="right" >
352
 
          <a href="stringio.html" title="7.5. StringIO — Read and write strings as files"
353
 
             >previous</a> |</li>
354
 
        <li><img src="../_static/py.png" alt=""
355
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
356
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
357
 
        <li>
358
 
          <span class="version_switcher_placeholder">2.7.11</span>
359
 
          <a href="../index.html">Documentation</a> &raquo;
360
 
        </li>
361
 
 
362
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
363
 
          <li class="nav-item nav-item-2"><a href="strings.html" >7. String Services</a> &raquo;</li> 
364
 
      </ul>
365
 
    </div>  
366
 
    <div class="footer">
367
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
368
 
    <br />
369
 
    The Python Software Foundation is a non-profit corporation.
370
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
371
 
    <br />
372
 
    Last updated on Jan 23, 2016.
373
 
    <a href="../bugs.html">Found a bug</a>?
374
 
    <br />
375
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
376
 
    </div>
377
 
 
378
 
  </body>
379
 
</html>
 
 
b'\\ No newline at end of file'