Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 9 | .. versionadded:: 2.3 |
| 10 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 11 | **Source code:** :source:`Lib/textwrap.py` |
| 12 | |
| 13 | -------------- |
| 14 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 15 | The :mod:`textwrap` module provides two convenience functions, :func:`wrap` and |
| 16 | :func:`fill`, as well as :class:`TextWrapper`, the class that does all the work, |
| 17 | and a utility function :func:`dedent`. If you're just wrapping or filling one |
| 18 | or two text strings, the convenience functions should be good enough; |
| 19 | otherwise, you should use an instance of :class:`TextWrapper` for efficiency. |
| 20 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 21 | .. function:: wrap(text[, width[, ...]]) |
| 22 | |
| 23 | Wraps the single paragraph in *text* (a string) so every line is at most *width* |
| 24 | characters long. Returns a list of output lines, without final newlines. |
| 25 | |
| 26 | Optional keyword arguments correspond to the instance attributes of |
| 27 | :class:`TextWrapper`, documented below. *width* defaults to ``70``. |
| 28 | |
R David Murray | 1b6e7c4 | 2012-09-08 13:42:01 -0400 | [diff] [blame] | 29 | See the :meth:`TextWrapper.wrap` method for additional details on how |
| 30 | :func:`wrap` behaves. |
| 31 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 32 | |
| 33 | .. function:: fill(text[, width[, ...]]) |
| 34 | |
| 35 | Wraps the single paragraph in *text*, and returns a single string containing the |
| 36 | wrapped paragraph. :func:`fill` is shorthand for :: |
| 37 | |
| 38 | "\n".join(wrap(text, ...)) |
| 39 | |
| 40 | In particular, :func:`fill` accepts exactly the same keyword arguments as |
| 41 | :func:`wrap`. |
| 42 | |
| 43 | Both :func:`wrap` and :func:`fill` work by creating a :class:`TextWrapper` |
| 44 | instance and calling a single method on it. That instance is not reused, so for |
| 45 | applications that wrap/fill many text strings, it will be more efficient for you |
| 46 | to create your own :class:`TextWrapper` object. |
| 47 | |
Georg Brandl | 6f95ae5 | 2008-05-11 10:42:28 +0000 | [diff] [blame] | 48 | Text is preferably wrapped on whitespaces and right after the hyphens in |
| 49 | hyphenated words; only then will long words be broken if necessary, unless |
| 50 | :attr:`TextWrapper.break_long_words` is set to false. |
| 51 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 52 | An additional utility function, :func:`dedent`, is provided to remove |
| 53 | indentation from strings that have unwanted whitespace to the left of the text. |
| 54 | |
| 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 |
| 65 | common leading whitespace. (This behaviour is new in Python 2.5; older versions |
| 66 | of this module incorrectly expanded tabs before searching for common leading |
| 67 | whitespace.) |
| 68 | |
| 69 | For example:: |
| 70 | |
| 71 | def test(): |
| 72 | # end first line with \ to avoid the empty line! |
| 73 | s = '''\ |
| 74 | hello |
| 75 | world |
| 76 | ''' |
| 77 | print repr(s) # prints ' hello\n world\n ' |
| 78 | print repr(dedent(s)) # prints 'hello\n world\n' |
| 79 | |
| 80 | |
| 81 | .. class:: TextWrapper(...) |
| 82 | |
| 83 | The :class:`TextWrapper` constructor accepts a number of optional keyword |
| 84 | arguments. Each argument corresponds to one instance attribute, so for example |
| 85 | :: |
| 86 | |
| 87 | wrapper = TextWrapper(initial_indent="* ") |
| 88 | |
| 89 | is the same as :: |
| 90 | |
| 91 | wrapper = TextWrapper() |
| 92 | wrapper.initial_indent = "* " |
| 93 | |
| 94 | You can re-use the same :class:`TextWrapper` object many times, and you can |
| 95 | change any of its options through direct assignment to instance attributes |
| 96 | between uses. |
| 97 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 98 | The :class:`TextWrapper` instance attributes (and keyword arguments to the |
| 99 | constructor) are as follows: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 100 | |
| 101 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 102 | .. attribute:: width |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 103 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 104 | (default: ``70``) The maximum length of wrapped lines. As long as there |
| 105 | are no individual words in the input text longer than :attr:`width`, |
| 106 | :class:`TextWrapper` guarantees that no output line will be longer than |
| 107 | :attr:`width` characters. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 108 | |
| 109 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 110 | .. attribute:: expand_tabs |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 111 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 112 | (default: ``True``) If true, then all tab characters in *text* will be |
| 113 | expanded to spaces using the :meth:`expandtabs` method of *text*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 114 | |
| 115 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 116 | .. attribute:: replace_whitespace |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 117 | |
Andrew Svetlov | 33f87a5 | 2012-08-13 23:26:28 +0300 | [diff] [blame] | 118 | (default: ``True``) If true, after tab expansion but before wrapping, |
| 119 | the :meth:`wrap` method will replace each whitespace character |
| 120 | with a single space. The whitespace characters replaced are |
| 121 | as follows: tab, newline, vertical tab, formfeed, and carriage |
| 122 | return (``'\t\n\v\f\r'``). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 123 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 124 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 126 | If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, |
| 127 | each tab character will be replaced by a single space, which is *not* |
| 128 | the same as tab expansion. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 129 | |
Terry Reedy | 61ff85e | 2010-11-23 20:28:34 +0000 | [diff] [blame] | 130 | .. note:: |
| 131 | |
| 132 | If :attr:`replace_whitespace` is false, newlines may appear in the |
| 133 | middle of a line and cause strange output. For this reason, text should |
| 134 | be split into paragraphs (using :meth:`str.splitlines` or similar) |
| 135 | which are wrapped separately. |
| 136 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 137 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 138 | .. attribute:: drop_whitespace |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 139 | |
R David Murray | 1b6e7c4 | 2012-09-08 13:42:01 -0400 | [diff] [blame] | 140 | (default: ``True``) If true, whitespace at the beginning and ending of |
| 141 | every line (after wrapping but before indenting) is dropped. |
| 142 | Whitespace at the beginning of the paragraph, however, is not dropped |
| 143 | if non-whitespace follows it. If whitespace being dropped takes up an |
| 144 | entire line, the whole line is dropped. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 145 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 146 | .. versionadded:: 2.6 |
| 147 | Whitespace was always dropped in earlier versions. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 148 | |
| 149 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 150 | .. attribute:: initial_indent |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 151 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 152 | (default: ``''``) String that will be prepended to the first line of |
R David Murray | 1b6e7c4 | 2012-09-08 13:42:01 -0400 | [diff] [blame] | 153 | wrapped output. Counts towards the length of the first line. The empty |
| 154 | string is not indented. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 155 | |
| 156 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 157 | .. attribute:: subsequent_indent |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 158 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 159 | (default: ``''``) String that will be prepended to all lines of wrapped |
| 160 | output except the first. Counts towards the length of each line except |
| 161 | the first. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 162 | |
| 163 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 164 | .. attribute:: fix_sentence_endings |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 165 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 166 | (default: ``False``) If true, :class:`TextWrapper` attempts to detect |
| 167 | sentence endings and ensure that sentences are always separated by exactly |
| 168 | two spaces. This is generally desired for text in a monospaced font. |
| 169 | However, the sentence detection algorithm is imperfect: it assumes that a |
| 170 | sentence ending consists of a lowercase letter followed by one of ``'.'``, |
| 171 | ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``, |
| 172 | followed by a space. One problem with this is algorithm is that it is |
| 173 | unable to detect the difference between "Dr." in :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 174 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 175 | [...] Dr. Frankenstein's monster [...] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 176 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 177 | and "Spot." in :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 178 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 179 | [...] See Spot. See Spot run [...] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 180 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 181 | :attr:`fix_sentence_endings` is false by default. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 182 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 183 | Since the sentence detection algorithm relies on ``string.lowercase`` for |
| 184 | the definition of "lowercase letter," and a convention of using two spaces |
| 185 | after a period to separate sentences on the same line, it is specific to |
| 186 | English-language texts. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 187 | |
| 188 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 189 | .. attribute:: break_long_words |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 190 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 191 | (default: ``True``) If true, then words longer than :attr:`width` will be |
| 192 | broken in order to ensure that no lines are longer than :attr:`width`. If |
| 193 | it is false, long words will not be broken, and some lines may be longer |
| 194 | than :attr:`width`. (Long words will be put on a line by themselves, in |
| 195 | order to minimize the amount by which :attr:`width` is exceeded.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 196 | |
Georg Brandl | 6f95ae5 | 2008-05-11 10:42:28 +0000 | [diff] [blame] | 197 | |
| 198 | .. attribute:: break_on_hyphens |
| 199 | |
| 200 | (default: ``True``) If true, wrapping will occur preferably on whitespaces |
| 201 | and right after hyphens in compound words, as it is customary in English. |
| 202 | If false, only whitespaces will be considered as potentially good places |
| 203 | for line breaks, but you need to set :attr:`break_long_words` to false if |
| 204 | you want truly insecable words. Default behaviour in previous versions |
| 205 | was to always allow breaking hyphenated words. |
| 206 | |
| 207 | .. versionadded:: 2.6 |
| 208 | |
| 209 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 210 | :class:`TextWrapper` also provides two public methods, analogous to the |
| 211 | module-level convenience functions: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 212 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 213 | .. method:: wrap(text) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 215 | Wraps the single paragraph in *text* (a string) so every line is at most |
| 216 | :attr:`width` characters long. All wrapping options are taken from |
R David Murray | 1b6e7c4 | 2012-09-08 13:42:01 -0400 | [diff] [blame] | 217 | instance attributes of the :class:`TextWrapper` instance. Returns a list |
| 218 | of output lines, without final newlines. If the wrapped output has no |
| 219 | content, the returned list is empty. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 220 | |
| 221 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 222 | .. method:: fill(text) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 223 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 224 | Wraps the single paragraph in *text*, and returns a single string |
| 225 | containing the wrapped paragraph. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 226 | |