17db96d56Sopenharmony_ci:mod:`textwrap` --- Text wrapping and filling 27db96d56Sopenharmony_ci============================================= 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: textwrap 57db96d56Sopenharmony_ci :synopsis: Text wrapping and filling 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. moduleauthor:: Greg Ward <gward@python.net> 87db96d56Sopenharmony_ci.. sectionauthor:: Greg Ward <gward@python.net> 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci**Source code:** :source:`Lib/textwrap.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci-------------- 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciThe :mod:`textwrap` module provides some convenience functions, 157db96d56Sopenharmony_cias well as :class:`TextWrapper`, the class that does all the work. 167db96d56Sopenharmony_ciIf you're just wrapping or filling one or two text strings, the convenience 177db96d56Sopenharmony_cifunctions should be good enough; otherwise, you should use an instance of 187db96d56Sopenharmony_ci:class:`TextWrapper` for efficiency. 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ci.. function:: wrap(text, width=70, *, initial_indent="", \ 217db96d56Sopenharmony_ci subsequent_indent="", expand_tabs=True, \ 227db96d56Sopenharmony_ci replace_whitespace=True, fix_sentence_endings=False, \ 237db96d56Sopenharmony_ci break_long_words=True, drop_whitespace=True, \ 247db96d56Sopenharmony_ci break_on_hyphens=True, tabsize=8, max_lines=None, \ 257db96d56Sopenharmony_ci placeholder=' [...]') 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ci Wraps the single paragraph in *text* (a string) so every line is at most 287db96d56Sopenharmony_ci *width* characters long. Returns a list of output lines, without final 297db96d56Sopenharmony_ci newlines. 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ci Optional keyword arguments correspond to the instance attributes of 327db96d56Sopenharmony_ci :class:`TextWrapper`, documented below. 337db96d56Sopenharmony_ci 347db96d56Sopenharmony_ci See the :meth:`TextWrapper.wrap` method for additional details on how 357db96d56Sopenharmony_ci :func:`wrap` behaves. 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci.. function:: fill(text, width=70, *, initial_indent="", \ 397db96d56Sopenharmony_ci subsequent_indent="", expand_tabs=True, \ 407db96d56Sopenharmony_ci replace_whitespace=True, fix_sentence_endings=False, \ 417db96d56Sopenharmony_ci break_long_words=True, drop_whitespace=True, \ 427db96d56Sopenharmony_ci break_on_hyphens=True, tabsize=8, \ 437db96d56Sopenharmony_ci max_lines=None, placeholder=' [...]') 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci Wraps the single paragraph in *text*, and returns a single string containing the 467db96d56Sopenharmony_ci wrapped paragraph. :func:`fill` is shorthand for :: 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci "\n".join(wrap(text, ...)) 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci In particular, :func:`fill` accepts exactly the same keyword arguments as 517db96d56Sopenharmony_ci :func:`wrap`. 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ci.. function:: shorten(text, width, *, fix_sentence_endings=False, \ 557db96d56Sopenharmony_ci break_long_words=True, break_on_hyphens=True, \ 567db96d56Sopenharmony_ci placeholder=' [...]') 577db96d56Sopenharmony_ci 587db96d56Sopenharmony_ci Collapse and truncate the given *text* to fit in the given *width*. 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci First the whitespace in *text* is collapsed (all whitespace is replaced by 617db96d56Sopenharmony_ci single spaces). If the result fits in the *width*, it is returned. 627db96d56Sopenharmony_ci Otherwise, enough words are dropped from the end so that the remaining words 637db96d56Sopenharmony_ci plus the :attr:`placeholder` fit within :attr:`width`:: 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ci >>> textwrap.shorten("Hello world!", width=12) 667db96d56Sopenharmony_ci 'Hello world!' 677db96d56Sopenharmony_ci >>> textwrap.shorten("Hello world!", width=11) 687db96d56Sopenharmony_ci 'Hello [...]' 697db96d56Sopenharmony_ci >>> textwrap.shorten("Hello world", width=10, placeholder="...") 707db96d56Sopenharmony_ci 'Hello...' 717db96d56Sopenharmony_ci 727db96d56Sopenharmony_ci Optional keyword arguments correspond to the instance attributes of 737db96d56Sopenharmony_ci :class:`TextWrapper`, documented below. Note that the whitespace is 747db96d56Sopenharmony_ci collapsed before the text is passed to the :class:`TextWrapper` :meth:`fill` 757db96d56Sopenharmony_ci function, so changing the value of :attr:`.tabsize`, :attr:`.expand_tabs`, 767db96d56Sopenharmony_ci :attr:`.drop_whitespace`, and :attr:`.replace_whitespace` will have no effect. 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci .. versionadded:: 3.4 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ci.. function:: dedent(text) 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ci Remove any common leading whitespace from every line in *text*. 837db96d56Sopenharmony_ci 847db96d56Sopenharmony_ci This can be used to make triple-quoted strings line up with the left edge of the 857db96d56Sopenharmony_ci display, while still presenting them in the source code in indented form. 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ci Note that tabs and spaces are both treated as whitespace, but they are not 887db96d56Sopenharmony_ci equal: the lines ``" hello"`` and ``"\thello"`` are considered to have no 897db96d56Sopenharmony_ci common leading whitespace. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci Lines containing only whitespace are ignored in the input and normalized to a 927db96d56Sopenharmony_ci single newline character in the output. 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci For example:: 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci def test(): 977db96d56Sopenharmony_ci # end first line with \ to avoid the empty line! 987db96d56Sopenharmony_ci s = '''\ 997db96d56Sopenharmony_ci hello 1007db96d56Sopenharmony_ci world 1017db96d56Sopenharmony_ci ''' 1027db96d56Sopenharmony_ci print(repr(s)) # prints ' hello\n world\n ' 1037db96d56Sopenharmony_ci print(repr(dedent(s))) # prints 'hello\n world\n' 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci.. function:: indent(text, prefix, predicate=None) 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci Add *prefix* to the beginning of selected lines in *text*. 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ci Lines are separated by calling ``text.splitlines(True)``. 1117db96d56Sopenharmony_ci 1127db96d56Sopenharmony_ci By default, *prefix* is added to all lines that do not consist 1137db96d56Sopenharmony_ci solely of whitespace (including any line endings). 1147db96d56Sopenharmony_ci 1157db96d56Sopenharmony_ci For example:: 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci >>> s = 'hello\n\n \nworld' 1187db96d56Sopenharmony_ci >>> indent(s, ' ') 1197db96d56Sopenharmony_ci ' hello\n\n \n world' 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ci The optional *predicate* argument can be used to control which lines 1227db96d56Sopenharmony_ci are indented. For example, it is easy to add *prefix* to even empty 1237db96d56Sopenharmony_ci and whitespace-only lines:: 1247db96d56Sopenharmony_ci 1257db96d56Sopenharmony_ci >>> print(indent(s, '+ ', lambda line: True)) 1267db96d56Sopenharmony_ci + hello 1277db96d56Sopenharmony_ci + 1287db96d56Sopenharmony_ci + 1297db96d56Sopenharmony_ci + world 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci .. versionadded:: 3.3 1327db96d56Sopenharmony_ci 1337db96d56Sopenharmony_ci 1347db96d56Sopenharmony_ci:func:`wrap`, :func:`fill` and :func:`shorten` work by creating a 1357db96d56Sopenharmony_ci:class:`TextWrapper` instance and calling a single method on it. That 1367db96d56Sopenharmony_ciinstance is not reused, so for applications that process many text 1377db96d56Sopenharmony_cistrings using :func:`wrap` and/or :func:`fill`, it may be more efficient to 1387db96d56Sopenharmony_cicreate your own :class:`TextWrapper` object. 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ciText is preferably wrapped on whitespaces and right after the hyphens in 1417db96d56Sopenharmony_cihyphenated words; only then will long words be broken if necessary, unless 1427db96d56Sopenharmony_ci:attr:`TextWrapper.break_long_words` is set to false. 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci.. class:: TextWrapper(**kwargs) 1457db96d56Sopenharmony_ci 1467db96d56Sopenharmony_ci The :class:`TextWrapper` constructor accepts a number of optional keyword 1477db96d56Sopenharmony_ci arguments. Each keyword argument corresponds to an instance attribute, so 1487db96d56Sopenharmony_ci for example :: 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci wrapper = TextWrapper(initial_indent="* ") 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ci is the same as :: 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ci wrapper = TextWrapper() 1557db96d56Sopenharmony_ci wrapper.initial_indent = "* " 1567db96d56Sopenharmony_ci 1577db96d56Sopenharmony_ci You can re-use the same :class:`TextWrapper` object many times, and you can 1587db96d56Sopenharmony_ci change any of its options through direct assignment to instance attributes 1597db96d56Sopenharmony_ci between uses. 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci The :class:`TextWrapper` instance attributes (and keyword arguments to the 1627db96d56Sopenharmony_ci constructor) are as follows: 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci 1657db96d56Sopenharmony_ci .. attribute:: width 1667db96d56Sopenharmony_ci 1677db96d56Sopenharmony_ci (default: ``70``) The maximum length of wrapped lines. As long as there 1687db96d56Sopenharmony_ci are no individual words in the input text longer than :attr:`width`, 1697db96d56Sopenharmony_ci :class:`TextWrapper` guarantees that no output line will be longer than 1707db96d56Sopenharmony_ci :attr:`width` characters. 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci .. attribute:: expand_tabs 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ci (default: ``True``) If true, then all tab characters in *text* will be 1767db96d56Sopenharmony_ci expanded to spaces using the :meth:`expandtabs` method of *text*. 1777db96d56Sopenharmony_ci 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci .. attribute:: tabsize 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci (default: ``8``) If :attr:`expand_tabs` is true, then all tab characters 1827db96d56Sopenharmony_ci in *text* will be expanded to zero or more spaces, depending on the 1837db96d56Sopenharmony_ci current column and the given tab size. 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci .. versionadded:: 3.3 1867db96d56Sopenharmony_ci 1877db96d56Sopenharmony_ci 1887db96d56Sopenharmony_ci .. attribute:: replace_whitespace 1897db96d56Sopenharmony_ci 1907db96d56Sopenharmony_ci (default: ``True``) If true, after tab expansion but before wrapping, 1917db96d56Sopenharmony_ci the :meth:`wrap` method will replace each whitespace character 1927db96d56Sopenharmony_ci with a single space. The whitespace characters replaced are 1937db96d56Sopenharmony_ci as follows: tab, newline, vertical tab, formfeed, and carriage 1947db96d56Sopenharmony_ci return (``'\t\n\v\f\r'``). 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_ci .. note:: 1977db96d56Sopenharmony_ci 1987db96d56Sopenharmony_ci If :attr:`expand_tabs` is false and :attr:`replace_whitespace` is true, 1997db96d56Sopenharmony_ci each tab character will be replaced by a single space, which is *not* 2007db96d56Sopenharmony_ci the same as tab expansion. 2017db96d56Sopenharmony_ci 2027db96d56Sopenharmony_ci .. note:: 2037db96d56Sopenharmony_ci 2047db96d56Sopenharmony_ci If :attr:`replace_whitespace` is false, newlines may appear in the 2057db96d56Sopenharmony_ci middle of a line and cause strange output. For this reason, text should 2067db96d56Sopenharmony_ci be split into paragraphs (using :meth:`str.splitlines` or similar) 2077db96d56Sopenharmony_ci which are wrapped separately. 2087db96d56Sopenharmony_ci 2097db96d56Sopenharmony_ci 2107db96d56Sopenharmony_ci .. attribute:: drop_whitespace 2117db96d56Sopenharmony_ci 2127db96d56Sopenharmony_ci (default: ``True``) If true, whitespace at the beginning and ending of 2137db96d56Sopenharmony_ci every line (after wrapping but before indenting) is dropped. 2147db96d56Sopenharmony_ci Whitespace at the beginning of the paragraph, however, is not dropped 2157db96d56Sopenharmony_ci if non-whitespace follows it. If whitespace being dropped takes up an 2167db96d56Sopenharmony_ci entire line, the whole line is dropped. 2177db96d56Sopenharmony_ci 2187db96d56Sopenharmony_ci 2197db96d56Sopenharmony_ci .. attribute:: initial_indent 2207db96d56Sopenharmony_ci 2217db96d56Sopenharmony_ci (default: ``''``) String that will be prepended to the first line of 2227db96d56Sopenharmony_ci wrapped output. Counts towards the length of the first line. The empty 2237db96d56Sopenharmony_ci string is not indented. 2247db96d56Sopenharmony_ci 2257db96d56Sopenharmony_ci 2267db96d56Sopenharmony_ci .. attribute:: subsequent_indent 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci (default: ``''``) String that will be prepended to all lines of wrapped 2297db96d56Sopenharmony_ci output except the first. Counts towards the length of each line except 2307db96d56Sopenharmony_ci the first. 2317db96d56Sopenharmony_ci 2327db96d56Sopenharmony_ci 2337db96d56Sopenharmony_ci .. attribute:: fix_sentence_endings 2347db96d56Sopenharmony_ci 2357db96d56Sopenharmony_ci (default: ``False``) If true, :class:`TextWrapper` attempts to detect 2367db96d56Sopenharmony_ci sentence endings and ensure that sentences are always separated by exactly 2377db96d56Sopenharmony_ci two spaces. This is generally desired for text in a monospaced font. 2387db96d56Sopenharmony_ci However, the sentence detection algorithm is imperfect: it assumes that a 2397db96d56Sopenharmony_ci sentence ending consists of a lowercase letter followed by one of ``'.'``, 2407db96d56Sopenharmony_ci ``'!'``, or ``'?'``, possibly followed by one of ``'"'`` or ``"'"``, 2417db96d56Sopenharmony_ci followed by a space. One problem with this is algorithm is that it is 2427db96d56Sopenharmony_ci unable to detect the difference between "Dr." in :: 2437db96d56Sopenharmony_ci 2447db96d56Sopenharmony_ci [...] Dr. Frankenstein's monster [...] 2457db96d56Sopenharmony_ci 2467db96d56Sopenharmony_ci and "Spot." in :: 2477db96d56Sopenharmony_ci 2487db96d56Sopenharmony_ci [...] See Spot. See Spot run [...] 2497db96d56Sopenharmony_ci 2507db96d56Sopenharmony_ci :attr:`fix_sentence_endings` is false by default. 2517db96d56Sopenharmony_ci 2527db96d56Sopenharmony_ci Since the sentence detection algorithm relies on ``string.lowercase`` for 2537db96d56Sopenharmony_ci the definition of "lowercase letter", and a convention of using two spaces 2547db96d56Sopenharmony_ci after a period to separate sentences on the same line, it is specific to 2557db96d56Sopenharmony_ci English-language texts. 2567db96d56Sopenharmony_ci 2577db96d56Sopenharmony_ci 2587db96d56Sopenharmony_ci .. attribute:: break_long_words 2597db96d56Sopenharmony_ci 2607db96d56Sopenharmony_ci (default: ``True``) If true, then words longer than :attr:`width` will be 2617db96d56Sopenharmony_ci broken in order to ensure that no lines are longer than :attr:`width`. If 2627db96d56Sopenharmony_ci it is false, long words will not be broken, and some lines may be longer 2637db96d56Sopenharmony_ci than :attr:`width`. (Long words will be put on a line by themselves, in 2647db96d56Sopenharmony_ci order to minimize the amount by which :attr:`width` is exceeded.) 2657db96d56Sopenharmony_ci 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci .. attribute:: break_on_hyphens 2687db96d56Sopenharmony_ci 2697db96d56Sopenharmony_ci (default: ``True``) If true, wrapping will occur preferably on whitespaces 2707db96d56Sopenharmony_ci and right after hyphens in compound words, as it is customary in English. 2717db96d56Sopenharmony_ci If false, only whitespaces will be considered as potentially good places 2727db96d56Sopenharmony_ci for line breaks, but you need to set :attr:`break_long_words` to false if 2737db96d56Sopenharmony_ci you want truly insecable words. Default behaviour in previous versions 2747db96d56Sopenharmony_ci was to always allow breaking hyphenated words. 2757db96d56Sopenharmony_ci 2767db96d56Sopenharmony_ci 2777db96d56Sopenharmony_ci .. attribute:: max_lines 2787db96d56Sopenharmony_ci 2797db96d56Sopenharmony_ci (default: ``None``) If not ``None``, then the output will contain at most 2807db96d56Sopenharmony_ci *max_lines* lines, with *placeholder* appearing at the end of the output. 2817db96d56Sopenharmony_ci 2827db96d56Sopenharmony_ci .. versionadded:: 3.4 2837db96d56Sopenharmony_ci 2847db96d56Sopenharmony_ci 2857db96d56Sopenharmony_ci .. index:: single: ...; placeholder 2867db96d56Sopenharmony_ci 2877db96d56Sopenharmony_ci .. attribute:: placeholder 2887db96d56Sopenharmony_ci 2897db96d56Sopenharmony_ci (default: ``' [...]'``) String that will appear at the end of the output 2907db96d56Sopenharmony_ci text if it has been truncated. 2917db96d56Sopenharmony_ci 2927db96d56Sopenharmony_ci .. versionadded:: 3.4 2937db96d56Sopenharmony_ci 2947db96d56Sopenharmony_ci 2957db96d56Sopenharmony_ci :class:`TextWrapper` also provides some public methods, analogous to the 2967db96d56Sopenharmony_ci module-level convenience functions: 2977db96d56Sopenharmony_ci 2987db96d56Sopenharmony_ci .. method:: wrap(text) 2997db96d56Sopenharmony_ci 3007db96d56Sopenharmony_ci Wraps the single paragraph in *text* (a string) so every line is at most 3017db96d56Sopenharmony_ci :attr:`width` characters long. All wrapping options are taken from 3027db96d56Sopenharmony_ci instance attributes of the :class:`TextWrapper` instance. Returns a list 3037db96d56Sopenharmony_ci of output lines, without final newlines. If the wrapped output has no 3047db96d56Sopenharmony_ci content, the returned list is empty. 3057db96d56Sopenharmony_ci 3067db96d56Sopenharmony_ci 3077db96d56Sopenharmony_ci .. method:: fill(text) 3087db96d56Sopenharmony_ci 3097db96d56Sopenharmony_ci Wraps the single paragraph in *text*, and returns a single string 3107db96d56Sopenharmony_ci containing the wrapped paragraph. 311