| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`textwrap` --- Text wrapping and filling | 
 | 2 | ============================================= | 
 | 3 |  | 
 | 4 | .. module:: textwrap | 
 | 5 |    :synopsis: Text wrapping and filling | 
 | 6 | .. moduleauthor:: Greg Ward <gward@python.net> | 
 | 7 | .. sectionauthor:: Greg Ward <gward@python.net> | 
 | 8 |  | 
| Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/textwrap.py` | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 |  | 
| Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 11 | -------------- | 
 | 12 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and | 
 | 14 | :func:`fill`, as well as :class:`TextWrapper`, the class that does all the work, | 
| Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 15 | and two utility functions, :func:`dedent` and :func:`indent`.  If you're just wrapping or filling one | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | or two  text strings, the convenience functions should be good enough; | 
 | 17 | otherwise,  you should use an instance of :class:`TextWrapper` for efficiency. | 
 | 18 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 19 | .. function:: wrap(text, width=70, **kwargs) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 21 |    Wraps the single paragraph in *text* (a string) so every line is at most | 
 | 22 |    *width* characters long.  Returns a list of output lines, without final | 
 | 23 |    newlines. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 |  | 
 | 25 |    Optional keyword arguments correspond to the instance attributes of | 
 | 26 |    :class:`TextWrapper`, documented below.  *width* defaults to ``70``. | 
 | 27 |  | 
| R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 28 |    See the :meth:`TextWrapper.wrap` method for additional details on how | 
 | 29 |    :func:`wrap` behaves. | 
 | 30 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 32 | .. function:: fill(text, width=70, **kwargs) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 |  | 
 | 34 |    Wraps the single paragraph in *text*, and returns a single string containing the | 
 | 35 |    wrapped paragraph.  :func:`fill` is shorthand for  :: | 
 | 36 |  | 
 | 37 |       "\n".join(wrap(text, ...)) | 
 | 38 |  | 
 | 39 |    In particular, :func:`fill` accepts exactly the same keyword arguments as | 
 | 40 |    :func:`wrap`. | 
 | 41 |  | 
 | 42 | Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper` | 
 | 43 | instance and calling a single method on it.  That instance is not reused, so for | 
 | 44 | applications that wrap/fill many text strings, it will be more efficient for you | 
 | 45 | to create your own :class:`TextWrapper` object. | 
 | 46 |  | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 47 | Text is preferably wrapped on whitespaces and right after the hyphens in | 
 | 48 | hyphenated words; only then will long words be broken if necessary, unless | 
 | 49 | :attr:`TextWrapper.break_long_words` is set to false. | 
 | 50 |  | 
| Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 51 | Two additional utility function, :func:`dedent` and :func:`indent`, are | 
 | 52 | provided to remove indentation from strings that have unwanted whitespace | 
 | 53 | to the left of the text and to add an arbitrary prefix to selected lines | 
 | 54 | in a block of text. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 |  | 
 | 56 | .. function:: dedent(text) | 
 | 57 |  | 
 | 58 |    Remove any common leading whitespace from every line in *text*. | 
 | 59 |  | 
 | 60 |    This can be used to make triple-quoted strings line up with the left edge of the | 
 | 61 |    display, while still presenting them in the source code in indented form. | 
 | 62 |  | 
 | 63 |    Note that tabs and spaces are both treated as whitespace, but they are not | 
 | 64 |    equal: the lines ``"  hello"`` and ``"\thello"`` are considered to have no | 
| Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 65 |    common leading whitespace. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 |  | 
 | 67 |    For example:: | 
 | 68 |  | 
 | 69 |       def test(): | 
 | 70 |           # end first line with \ to avoid the empty line! | 
 | 71 |           s = '''\ | 
 | 72 |           hello | 
 | 73 |             world | 
 | 74 |           ''' | 
| Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 75 |           print(repr(s))          # prints '    hello\n      world\n    ' | 
 | 76 |           print(repr(dedent(s)))  # prints 'hello\n  world\n' | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 |  | 
 | 78 |  | 
| Nick Coghlan | 4fae8cd | 2012-06-11 23:07:51 +1000 | [diff] [blame] | 79 | .. function:: indent(text, prefix, predicate=None) | 
 | 80 |  | 
 | 81 |    Add *prefix* to the beginning of selected lines in *text*. | 
 | 82 |  | 
 | 83 |    Lines are separated by calling ``text.splitlines(True)``. | 
 | 84 |  | 
 | 85 |    By default, *prefix* is added to all lines that do not consist | 
 | 86 |    solely of whitespace (including any line endings). | 
 | 87 |  | 
 | 88 |    For example:: | 
 | 89 |  | 
 | 90 |       >>> s = 'hello\n\n \nworld' | 
 | 91 |       >>> indent(s, '  ') | 
 | 92 |       '  hello\n\n \n  world' | 
 | 93 |  | 
 | 94 |    The optional *predicate* argument can be used to control which lines | 
 | 95 |    are indented. For example, it is easy to add *prefix* to even empty | 
 | 96 |    and whitespace-only lines:: | 
 | 97 |  | 
 | 98 |       >>> print(indent(s, '+ ', lambda line: True)) | 
 | 99 |       + hello | 
 | 100 |       + | 
 | 101 |       + | 
 | 102 |       + world | 
 | 103 |  | 
 | 104 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 105 | .. class:: TextWrapper(**kwargs) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 |  | 
 | 107 |    The :class:`TextWrapper` constructor accepts a number of optional keyword | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 108 |    arguments.  Each keyword argument corresponds to an instance attribute, so | 
 | 109 |    for example :: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 |  | 
 | 111 |       wrapper = TextWrapper(initial_indent="* ") | 
 | 112 |  | 
 | 113 |    is the same as  :: | 
 | 114 |  | 
 | 115 |       wrapper = TextWrapper() | 
 | 116 |       wrapper.initial_indent = "* " | 
 | 117 |  | 
 | 118 |    You can re-use the same :class:`TextWrapper` object many times, and you can | 
 | 119 |    change any of its options through direct assignment to instance attributes | 
 | 120 |    between uses. | 
 | 121 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 122 |    The :class:`TextWrapper` instance attributes (and keyword arguments to the | 
 | 123 |    constructor) are as follows: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 |  | 
 | 125 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 126 |    .. attribute:: width | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 128 |       (default: ``70``) The maximum length of wrapped lines.  As long as there | 
 | 129 |       are no individual words in the input text longer than :attr:`width`, | 
 | 130 |       :class:`TextWrapper` guarantees that no output line will be longer than | 
 | 131 |       :attr:`width` characters. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 132 |  | 
 | 133 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 134 |    .. attribute:: expand_tabs | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 136 |       (default: ``True``) If true, then all tab characters in *text* will be | 
 | 137 |       expanded to spaces using the :meth:`expandtabs` method of *text*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 |  | 
 | 139 |  | 
| Hynek Schlawack | d527259 | 2012-05-19 13:33:11 +0200 | [diff] [blame] | 140 |    .. attribute:: tabsize | 
 | 141 |  | 
 | 142 |       (default: ``8``) If :attr:`expand_tabs` is true, then all tab characters | 
 | 143 |       in *text* will be expanded to zero or more spaces, depending on the | 
 | 144 |       current column and the given tab size. | 
 | 145 |  | 
 | 146 |       .. versionadded:: 3.3 | 
 | 147 |  | 
 | 148 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 149 |    .. attribute:: replace_whitespace | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 |  | 
| Andrew Svetlov | 59db401 | 2012-08-13 23:22:23 +0300 | [diff] [blame] | 151 |       (default: ``True``) If true, after tab expansion but before wrapping, | 
 | 152 |       the :meth:`wrap` method will replace each whitespace character | 
 | 153 |       with a single space.  The whitespace characters replaced are | 
 | 154 |       as follows: tab, newline, vertical tab, formfeed, and carriage | 
 | 155 |       return (``'\t\n\v\f\r'``). | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 157 |       .. note:: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 159 |          If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, | 
 | 160 |          each tab character will be replaced by a single space, which is *not* | 
 | 161 |          the same as tab expansion. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 |  | 
| Terry Reedy | 6d2ab71 | 2010-11-23 20:17:24 +0000 | [diff] [blame] | 163 |       .. note:: | 
 | 164 |  | 
 | 165 |          If :attr:`replace_whitespace` is false, newlines may appear in the | 
 | 166 |          middle of a line and cause strange output. For this reason, text should | 
 | 167 |          be split into paragraphs (using :meth:`str.splitlines` or similar) | 
 | 168 |          which are wrapped separately. | 
 | 169 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 171 |    .. attribute:: drop_whitespace | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 |  | 
| R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 173 |       (default: ``True``) If true, whitespace at the beginning and ending of | 
 | 174 |       every line (after wrapping but before indenting) is dropped. | 
 | 175 |       Whitespace at the beginning of the paragraph, however, is not dropped | 
 | 176 |       if non-whitespace follows it.  If whitespace being dropped takes up an | 
 | 177 |       entire line, the whole line is dropped. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 178 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 180 |    .. attribute:: initial_indent | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 182 |       (default: ``''``) String that will be prepended to the first line of | 
| R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 183 |       wrapped output.  Counts towards the length of the first line.  The empty | 
 | 184 |       string is not indented. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 185 |  | 
 | 186 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 187 |    .. attribute:: subsequent_indent | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 189 |       (default: ``''``) String that will be prepended to all lines of wrapped | 
 | 190 |       output except the first.  Counts towards the length of each line except | 
 | 191 |       the first. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 |  | 
 | 193 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 194 |    .. attribute:: fix_sentence_endings | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 196 |       (default: ``False``) If true, :class:`TextWrapper` attempts to detect | 
 | 197 |       sentence endings and ensure that sentences are always separated by exactly | 
 | 198 |       two spaces.  This is generally desired for text in a monospaced font. | 
 | 199 |       However, the sentence detection algorithm is imperfect: it assumes that a | 
 | 200 |       sentence ending consists of a lowercase letter followed by one of ``'.'``, | 
 | 201 |       ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``, | 
 | 202 |       followed by a space.  One problem with this is algorithm is that it is | 
 | 203 |       unable to detect the difference between "Dr." in :: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 205 |          [...] Dr. Frankenstein's monster [...] | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 207 |       and "Spot." in :: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 209 |          [...] See Spot. See Spot run [...] | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 211 |       :attr:`fix_sentence_endings` is false by default. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 213 |       Since the sentence detection algorithm relies on ``string.lowercase`` for | 
 | 214 |       the definition of "lowercase letter," and a convention of using two spaces | 
 | 215 |       after a period to separate sentences on the same line, it is specific to | 
 | 216 |       English-language texts. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 |  | 
 | 218 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 219 |    .. attribute:: break_long_words | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 221 |       (default: ``True``) If true, then words longer than :attr:`width` will be | 
 | 222 |       broken in order to ensure that no lines are longer than :attr:`width`.  If | 
 | 223 |       it is false, long words will not be broken, and some lines may be longer | 
 | 224 |       than :attr:`width`.  (Long words will be put on a line by themselves, in | 
 | 225 |       order to minimize the amount by which :attr:`width` is exceeded.) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 226 |  | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 227 |  | 
 | 228 |    .. attribute:: break_on_hyphens | 
 | 229 |  | 
 | 230 |       (default: ``True``) If true, wrapping will occur preferably on whitespaces | 
 | 231 |       and right after hyphens in compound words, as it is customary in English. | 
 | 232 |       If false, only whitespaces will be considered as potentially good places | 
 | 233 |       for line breaks, but you need to set :attr:`break_long_words` to false if | 
 | 234 |       you want truly insecable words.  Default behaviour in previous versions | 
 | 235 |       was to always allow breaking hyphenated words. | 
 | 236 |  | 
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 237 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 238 |    :class:`TextWrapper` also provides two public methods, analogous to the | 
 | 239 |    module-level convenience functions: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 241 |    .. method:: wrap(text) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 243 |       Wraps the single paragraph in *text* (a string) so every line is at most | 
 | 244 |       :attr:`width` characters long.  All wrapping options are taken from | 
| R David Murray | 1585b70 | 2012-09-08 13:13:25 -0400 | [diff] [blame] | 245 |       instance attributes of the :class:`TextWrapper` instance.  Returns a list | 
 | 246 |       of output lines, without final newlines.  If the wrapped output has no | 
 | 247 |       content, the returned list is empty. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 |  | 
 | 249 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 250 |    .. method:: fill(text) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 252 |       Wraps the single paragraph in *text*, and returns a single string | 
 | 253 |       containing the wrapped paragraph. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 |  |