| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-codecs.html" /> |
| 13 | <link rel="prev" href="module-cStringIO.html" /> |
| 14 | <link rel="parent" href="strings.html" /> |
| 15 | <link rel="next" href="module-codecs.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>4.8 textwrap -- Text wrapping and filling</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="4.7 cStringIO " |
| 25 | href="module-cStringIO.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="4. String Services" |
| 28 | href="strings.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="4.9 codecs " |
| 31 | href="module-codecs.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-cStringIO.html">4.7 cStringIO </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="strings.html">4. String Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="module-codecs.html">4.9 codecs </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION006800000000000000000"> |
| 56 | 4.8 <tt class="module">textwrap</tt> -- |
| 57 | Text wrapping and filling</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-textwrap"></A> |
| 62 | |
| 63 | <P> |
| 64 | |
| 65 | <span class="versionnote">New in version 2.3.</span> |
| 66 | |
| 67 | <P> |
| 68 | The <tt class="module">textwrap</tt> module provides two convenience functions, |
| 69 | <tt class="function">wrap()</tt> and <tt class="function">fill()</tt>, as well as |
| 70 | <tt class="class">TextWrapper</tt>, the class that does all the work, and a utility function |
| 71 | <tt class="function">dedent()</tt>. If you're just wrapping or filling one or two |
| 72 | text strings, the convenience functions should be good enough; otherwise, |
| 73 | you should use an instance of <tt class="class">TextWrapper</tt> for efficiency. |
| 74 | |
| 75 | <P> |
| 76 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 77 | <td><nobr><b><tt id='l2h-961' xml:id='l2h-961' class="function">wrap</tt></b>(</nobr></td> |
| 78 | <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> |
| 79 | <dd> |
| 80 | Wraps the single paragraph in <var>text</var> (a string) so every line is at |
| 81 | most <var>width</var> characters long. Returns a list of output lines, |
| 82 | without final newlines. |
| 83 | |
| 84 | <P> |
| 85 | Optional keyword arguments correspond to the instance attributes of |
| 86 | <tt class="class">TextWrapper</tt>, documented below. <var>width</var> defaults to |
| 87 | <code>70</code>. |
| 88 | </dl> |
| 89 | |
| 90 | <P> |
| 91 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 92 | <td><nobr><b><tt id='l2h-962' xml:id='l2h-962' class="function">fill</tt></b>(</nobr></td> |
| 93 | <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> |
| 94 | <dd> |
| 95 | Wraps the single paragraph in <var>text</var>, and returns a single string |
| 96 | containing the wrapped paragraph. <tt class="function">fill()</tt> is shorthand for |
| 97 | <div class="verbatim"><pre> |
| 98 | "\n".join(wrap(text, ...)) |
| 99 | </pre></div> |
| 100 | |
| 101 | <P> |
| 102 | In particular, <tt class="function">fill()</tt> accepts exactly the same keyword |
| 103 | arguments as <tt class="function">wrap()</tt>. |
| 104 | </dl> |
| 105 | |
| 106 | <P> |
| 107 | Both <tt class="function">wrap()</tt> and <tt class="function">fill()</tt> work by creating a |
| 108 | <tt class="class">TextWrapper</tt> instance and calling a single method on it. That |
| 109 | instance is not reused, so for applications that wrap/fill many text |
| 110 | strings, it will be more efficient for you to create your own |
| 111 | <tt class="class">TextWrapper</tt> object. |
| 112 | |
| 113 | <P> |
| 114 | An additional utility function, <tt class="function">dedent()</tt>, is provided to |
| 115 | remove indentation from strings that have unwanted whitespace to the |
| 116 | left of the text. |
| 117 | |
| 118 | <P> |
| 119 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 120 | <td><nobr><b><tt id='l2h-963' xml:id='l2h-963' class="function">dedent</tt></b>(</nobr></td> |
| 121 | <td><var>text</var>)</td></tr></table></dt> |
| 122 | <dd> |
| 123 | Remove any whitespace that can be uniformly removed from the left |
| 124 | of every line in <var>text</var>. |
| 125 | |
| 126 | <P> |
| 127 | This is typically used to make triple-quoted strings line up with |
| 128 | the left edge of screen/whatever, while still presenting it in the |
| 129 | source code in indented form. |
| 130 | |
| 131 | <P> |
| 132 | For example: |
| 133 | <div class="verbatim"><pre> |
| 134 | def test(): |
| 135 | # end first line with \ to avoid the empty line! |
| 136 | s = '''\ |
| 137 | hello |
| 138 | world |
| 139 | ''' |
| 140 | print repr(s) # prints ' hello\n world\n ' |
| 141 | print repr(dedent(s)) # prints 'hello\n world\n' |
| 142 | </pre></div> |
| 143 | </dl> |
| 144 | |
| 145 | <P> |
| 146 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 147 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-964' xml:id='l2h-964' class="class">TextWrapper</tt></b>(</nobr></td> |
| 148 | <td><var>...</var>)</td></tr></table></dt> |
| 149 | <dd> |
| 150 | The <tt class="class">TextWrapper</tt> constructor accepts a number of optional |
| 151 | keyword arguments. Each argument corresponds to one instance attribute, |
| 152 | so for example |
| 153 | <div class="verbatim"><pre> |
| 154 | wrapper = TextWrapper(initial_indent="* ") |
| 155 | </pre></div> |
| 156 | is the same as |
| 157 | <div class="verbatim"><pre> |
| 158 | wrapper = TextWrapper() |
| 159 | wrapper.initial_indent = "* " |
| 160 | </pre></div> |
| 161 | |
| 162 | <P> |
| 163 | You can re-use the same <tt class="class">TextWrapper</tt> object many times, and you |
| 164 | can change any of its options through direct assignment to instance |
| 165 | attributes between uses. |
| 166 | </dl> |
| 167 | |
| 168 | <P> |
| 169 | The <tt class="class">TextWrapper</tt> instance attributes (and keyword arguments to |
| 170 | the constructor) are as follows: |
| 171 | |
| 172 | <P> |
| 173 | <dl><dt><b><tt id='l2h-965' xml:id='l2h-965' class="member">width</tt></b></dt> |
| 174 | <dd> |
| 175 | (default: <code>70</code>) The maximum length of wrapped lines. As long as |
| 176 | there are no individual words in the input text longer than |
| 177 | <tt class="member">width</tt>, <tt class="class">TextWrapper</tt> guarantees that no output line |
| 178 | will be longer than <tt class="member">width</tt> characters. |
| 179 | </dl> |
| 180 | |
| 181 | <P> |
| 182 | <dl><dt><b><tt id='l2h-966' xml:id='l2h-966' class="member">expand_tabs</tt></b></dt> |
| 183 | <dd> |
| 184 | (default: <code>True</code>) If true, then all tab characters in <var>text</var> |
| 185 | will be expanded to spaces using the <tt class="method">expandtabs()</tt> method of |
| 186 | <var>text</var>. |
| 187 | </dl> |
| 188 | |
| 189 | <P> |
| 190 | <dl><dt><b><tt id='l2h-967' xml:id='l2h-967' class="member">replace_whitespace</tt></b></dt> |
| 191 | <dd> |
| 192 | (default: <code>True</code>) If true, each whitespace character (as defined |
| 193 | by <code>string.whitespace</code>) remaining after tab expansion will be |
| 194 | replaced by a single space. <span class="note"><b class="label">Note:</b> |
| 195 | If <tt class="member">expand_tabs</tt> is false |
| 196 | and <tt class="member">replace_whitespace</tt> is true, each tab character will be |
| 197 | replaced by a single space, which is <em>not</em> the same as tab |
| 198 | expansion.</span> |
| 199 | </dl> |
| 200 | |
| 201 | <P> |
| 202 | <dl><dt><b><tt id='l2h-968' xml:id='l2h-968' class="member">initial_indent</tt></b></dt> |
| 203 | <dd> |
| 204 | (default: <code>''</code>) String that will be prepended to the first line |
| 205 | of wrapped output. Counts towards the length of the first line. |
| 206 | </dl> |
| 207 | |
| 208 | <P> |
| 209 | <dl><dt><b><tt id='l2h-969' xml:id='l2h-969' class="member">subsequent_indent</tt></b></dt> |
| 210 | <dd> |
| 211 | (default: <code>''</code>) String that will be prepended to all lines of |
| 212 | wrapped output except the first. Counts towards the length of each |
| 213 | line except the first. |
| 214 | </dl> |
| 215 | |
| 216 | <P> |
| 217 | <dl><dt><b><tt id='l2h-970' xml:id='l2h-970' class="member">fix_sentence_endings</tt></b></dt> |
| 218 | <dd> |
| 219 | (default: <code>False</code>) If true, <tt class="class">TextWrapper</tt> attempts to detect |
| 220 | sentence endings and ensure that sentences are always separated by |
| 221 | exactly two spaces. This is generally desired for text in a monospaced |
| 222 | font. However, the sentence detection algorithm is imperfect: it |
| 223 | assumes that a sentence ending consists of a lowercase letter followed |
| 224 | by one of "<tt class="character">.</tt>", |
| 225 | "<tt class="character">!</tt>", or "<tt class="character">?</tt>", possibly followed by one of |
| 226 | "<tt class="character">"</tt>" or "<tt class="character">'</tt>", followed by a space. One problem |
| 227 | with this is algorithm is that it is unable to detect the difference |
| 228 | between ``Dr.'' in |
| 229 | |
| 230 | <P> |
| 231 | <div class="verbatim"><pre> |
| 232 | [...] Dr. Frankenstein's monster [...] |
| 233 | </pre></div> |
| 234 | |
| 235 | <P> |
| 236 | and ``Spot.'' in |
| 237 | |
| 238 | <P> |
| 239 | <div class="verbatim"><pre> |
| 240 | [...] See Spot. See Spot run [...] |
| 241 | </pre></div> |
| 242 | |
| 243 | <P> |
| 244 | <tt class="member">fix_sentence_endings</tt> is false by default. |
| 245 | |
| 246 | <P> |
| 247 | Since the sentence detection algorithm relies on |
| 248 | <code>string.lowercase</code> for the definition of ``lowercase letter,'' |
| 249 | and a convention of using two spaces after a period to separate |
| 250 | sentences on the same line, it is specific to English-language texts. |
| 251 | </dl> |
| 252 | |
| 253 | <P> |
| 254 | <dl><dt><b><tt id='l2h-971' xml:id='l2h-971' class="member">break_long_words</tt></b></dt> |
| 255 | <dd> |
| 256 | (default: <code>True</code>) If true, then words longer than |
| 257 | <tt class="member">width</tt> will be broken in order to ensure that no lines are |
| 258 | longer than <tt class="member">width</tt>. If it is false, long words will not be |
| 259 | broken, and some lines may be longer than <tt class="member">width</tt>. (Long words |
| 260 | will be put on a line by themselves, in order to minimize the amount |
| 261 | by which <tt class="member">width</tt> is exceeded.) |
| 262 | </dl> |
| 263 | |
| 264 | <P> |
| 265 | <tt class="class">TextWrapper</tt> also provides two public methods, analogous to the |
| 266 | module-level convenience functions: |
| 267 | |
| 268 | <P> |
| 269 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 270 | <td><nobr><b><tt id='l2h-972' xml:id='l2h-972' class="method">wrap</tt></b>(</nobr></td> |
| 271 | <td><var>text</var>)</td></tr></table></dt> |
| 272 | <dd> |
| 273 | Wraps the single paragraph in <var>text</var> (a string) so every line is |
| 274 | at most <tt class="member">width</tt> characters long. All wrapping options are |
| 275 | taken from instance attributes of the <tt class="class">TextWrapper</tt> instance. |
| 276 | Returns a list of output lines, without final newlines. |
| 277 | </dl> |
| 278 | |
| 279 | <P> |
| 280 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 281 | <td><nobr><b><tt id='l2h-973' xml:id='l2h-973' class="method">fill</tt></b>(</nobr></td> |
| 282 | <td><var>text</var>)</td></tr></table></dt> |
| 283 | <dd> |
| 284 | Wraps the single paragraph in <var>text</var>, and returns a single string |
| 285 | containing the wrapped paragraph. |
| 286 | </dl> |
| 287 | |
| 288 | <DIV CLASS="navigation"> |
| 289 | <div class='online-navigation'> |
| 290 | <p></p><hr /> |
| 291 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 292 | <tr> |
| 293 | <td class='online-navigation'><a rel="prev" title="4.7 cStringIO " |
| 294 | href="module-cStringIO.html"><img src='../icons/previous.png' |
| 295 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 296 | <td class='online-navigation'><a rel="parent" title="4. String Services" |
| 297 | href="strings.html"><img src='../icons/up.png' |
| 298 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 299 | <td class='online-navigation'><a rel="next" title="4.9 codecs " |
| 300 | href="module-codecs.html"><img src='../icons/next.png' |
| 301 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 302 | <td align="center" width="100%">Python Library Reference</td> |
| 303 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 304 | href="contents.html"><img src='../icons/contents.png' |
| 305 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 306 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 307 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 308 | <td class='online-navigation'><a rel="index" title="Index" |
| 309 | href="genindex.html"><img src='../icons/index.png' |
| 310 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 311 | </tr></table> |
| 312 | <div class='online-navigation'> |
| 313 | <b class="navlabel">Previous:</b> |
| 314 | <a class="sectref" rel="prev" href="module-cStringIO.html">4.7 cStringIO </A> |
| 315 | <b class="navlabel">Up:</b> |
| 316 | <a class="sectref" rel="parent" href="strings.html">4. String Services</A> |
| 317 | <b class="navlabel">Next:</b> |
| 318 | <a class="sectref" rel="next" href="module-codecs.html">4.9 codecs </A> |
| 319 | </div> |
| 320 | </div> |
| 321 | <hr /> |
| 322 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 323 | </DIV> |
| 324 | <!--End of Navigation Panel--> |
| 325 | <ADDRESS> |
| 326 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 327 | </ADDRESS> |
| 328 | </BODY> |
| 329 | </HTML> |