1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
<span id="textwrap-text-wrapping-and-filling"></span><h1>textwrap — Text wrapping and filling</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/textwrap.py">Lib/textwrap.py</a></p> <p>The <a class="reference internal" href="#module-textwrap" title="textwrap: Text wrapping and filling"><code>textwrap</code></a> module provides some convenience functions, as well as <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a>, the class that does all the work. If you’re just wrapping or filling one or two text strings, the convenience functions should be good enough; otherwise, you should use an instance of <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> for efficiency.</p> <dl class="py function"> <dt class="sig sig-object py" id="textwrap.wrap">
<code>textwrap.wrap(text, width=70, *, initial_indent='', subsequent_indent='', expand_tabs=True, replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, drop_whitespace=True, break_on_hyphens=True, tabsize=8, max_lines=None, placeholder=' [...]')</code> </dt> <dd>
<p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most <em>width</em> characters long. Returns a list of output lines, without final newlines.</p> <p>Optional keyword arguments correspond to the instance attributes of <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a>, documented below.</p> <p>See the <a class="reference internal" href="#textwrap.TextWrapper.wrap" title="textwrap.TextWrapper.wrap"><code>TextWrapper.wrap()</code></a> method for additional details on how <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code>wrap()</code></a> behaves.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="textwrap.fill">
<code>textwrap.fill(text, width=70, *, initial_indent='', subsequent_indent='', expand_tabs=True, replace_whitespace=True, fix_sentence_endings=False, break_long_words=True, drop_whitespace=True, break_on_hyphens=True, tabsize=8, max_lines=None, placeholder=' [...]')</code> </dt> <dd>
<p>Wraps the single paragraph in <em>text</em>, and returns a single string containing the wrapped paragraph. <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code>fill()</code></a> is shorthand for</p> <pre data-language="python">"\n".join(wrap(text, ...))
</pre> <p>In particular, <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code>fill()</code></a> accepts exactly the same keyword arguments as <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code>wrap()</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="textwrap.shorten">
<code>textwrap.shorten(text, width, *, fix_sentence_endings=False, break_long_words=True, break_on_hyphens=True, placeholder=' [...]')</code> </dt> <dd>
<p>Collapse and truncate the given <em>text</em> to fit in the given <em>width</em>.</p> <p>First the whitespace in <em>text</em> is collapsed (all whitespace is replaced by single spaces). If the result fits in the <em>width</em>, it is returned. Otherwise, enough words are dropped from the end so that the remaining words plus the <em>placeholder</em> fit within <em>width</em>:</p> <pre data-language="python">>>> textwrap.shorten("Hello world!", width=12)
'Hello world!'
>>> textwrap.shorten("Hello world!", width=11)
'Hello [...]'
>>> textwrap.shorten("Hello world", width=10, placeholder="...")
'Hello...'
</pre> <p>Optional keyword arguments correspond to the instance attributes of <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a>, documented below. Note that the whitespace is collapsed before the text is passed to the <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code>fill()</code></a> function, so changing the value of <a class="reference internal" href="#textwrap.TextWrapper.tabsize" title="textwrap.TextWrapper.tabsize"><code>tabsize</code></a>, <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code>expand_tabs</code></a>, <a class="reference internal" href="#textwrap.TextWrapper.drop_whitespace" title="textwrap.TextWrapper.drop_whitespace"><code>drop_whitespace</code></a>, and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code>replace_whitespace</code></a> will have no effect.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="textwrap.dedent">
<code>textwrap.dedent(text)</code> </dt> <dd>
<p>Remove any common leading whitespace from every line in <em>text</em>.</p> <p>This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form.</p> <p>Note that tabs and spaces are both treated as whitespace, but they are not equal: the lines <code>" hello"</code> and <code>"\thello"</code> are considered to have no common leading whitespace.</p> <p>Lines containing only whitespace are ignored in the input and normalized to a single newline character in the output.</p> <p>For example:</p> <pre data-language="python">def test():
# end first line with \ to avoid the empty line!
s = '''\
hello
world
'''
print(repr(s)) # prints ' hello\n world\n '
print(repr(dedent(s))) # prints 'hello\n world\n'
</pre> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="textwrap.indent">
<code>textwrap.indent(text, prefix, predicate=None)</code> </dt> <dd>
<p>Add <em>prefix</em> to the beginning of selected lines in <em>text</em>.</p> <p>Lines are separated by calling <code>text.splitlines(True)</code>.</p> <p>By default, <em>prefix</em> is added to all lines that do not consist solely of whitespace (including any line endings).</p> <p>For example:</p> <pre data-language="python">>>> s = 'hello\n\n \nworld'
>>> indent(s, ' ')
' hello\n\n \n world'
</pre> <p>The optional <em>predicate</em> argument can be used to control which lines are indented. For example, it is easy to add <em>prefix</em> to even empty and whitespace-only lines:</p> <pre data-language="python">>>> print(indent(s, '+ ', lambda line: True))
+ hello
+
+
+ world
</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <p><a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code>wrap()</code></a>, <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code>fill()</code></a> and <a class="reference internal" href="#textwrap.shorten" title="textwrap.shorten"><code>shorten()</code></a> work by creating a <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> instance and calling a single method on it. That instance is not reused, so for applications that process many text strings using <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code>wrap()</code></a> and/or <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code>fill()</code></a>, it may be more efficient to create your own <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> object.</p> <p>Text is preferably wrapped on whitespaces and right after the hyphens in hyphenated words; only then will long words be broken if necessary, unless <a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><code>TextWrapper.break_long_words</code></a> is set to false.</p> <dl class="py class"> <dt class="sig sig-object py" id="textwrap.TextWrapper">
<code>class textwrap.TextWrapper(**kwargs)</code> </dt> <dd>
<p>The <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> constructor accepts a number of optional keyword arguments. Each keyword argument corresponds to an instance attribute, so for example</p> <pre data-language="python">wrapper = TextWrapper(initial_indent="* ")
</pre> <p>is the same as</p> <pre data-language="python">wrapper = TextWrapper()
wrapper.initial_indent = "* "
</pre> <p>You can re-use the same <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> object many times, and you can change any of its options through direct assignment to instance attributes between uses.</p> <p>The <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> instance attributes (and keyword arguments to the constructor) are as follows:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.width">
<code>width</code> </dt> <dd>
<p>(default: <code>70</code>) The maximum length of wrapped lines. As long as there are no individual words in the input text longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code>width</code></a>, <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> guarantees that no output line will be longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code>width</code></a> characters.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.expand_tabs">
<code>expand_tabs</code> </dt> <dd>
<p>(default: <code>True</code>) If true, then all tab characters in <em>text</em> will be expanded to spaces using the <a class="reference internal" href="stdtypes#str.expandtabs" title="str.expandtabs"><code>expandtabs()</code></a> method of <em>text</em>.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.tabsize">
<code>tabsize</code> </dt> <dd>
<p>(default: <code>8</code>) If <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code>expand_tabs</code></a> is true, then all tab characters in <em>text</em> will be expanded to zero or more spaces, depending on the current column and the given tab size.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.replace_whitespace">
<code>replace_whitespace</code> </dt> <dd>
<p>(default: <code>True</code>) If true, after tab expansion but before wrapping, the <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code>wrap()</code></a> method will replace each whitespace character with a single space. The whitespace characters replaced are as follows: tab, newline, vertical tab, formfeed, and carriage return (<code>'\t\n\v\f\r'</code>).</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code>expand_tabs</code></a> is false and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code>replace_whitespace</code></a> is true, each tab character will be replaced by a single space, which is <em>not</em> the same as tab expansion.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code>replace_whitespace</code></a> is false, newlines may appear in the middle of a line and cause strange output. For this reason, text should be split into paragraphs (using <a class="reference internal" href="stdtypes#str.splitlines" title="str.splitlines"><code>str.splitlines()</code></a> or similar) which are wrapped separately.</p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.drop_whitespace">
<code>drop_whitespace</code> </dt> <dd>
<p>(default: <code>True</code>) If true, whitespace at the beginning and ending of every line (after wrapping but before indenting) is dropped. Whitespace at the beginning of the paragraph, however, is not dropped if non-whitespace follows it. If whitespace being dropped takes up an entire line, the whole line is dropped.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.initial_indent">
<code>initial_indent</code> </dt> <dd>
<p>(default: <code>''</code>) String that will be prepended to the first line of wrapped output. Counts towards the length of the first line. The empty string is not indented.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.subsequent_indent">
<code>subsequent_indent</code> </dt> <dd>
<p>(default: <code>''</code>) String that will be prepended to all lines of wrapped output except the first. Counts towards the length of each line except the first.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.fix_sentence_endings">
<code>fix_sentence_endings</code> </dt> <dd>
<p>(default: <code>False</code>) If true, <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> attempts to detect sentence endings and ensure that sentences are always separated by exactly two spaces. This is generally desired for text in a monospaced font. However, the sentence detection algorithm is imperfect: it assumes that a sentence ending consists of a lowercase letter followed by one of <code>'.'</code>, <code>'!'</code>, or <code>'?'</code>, possibly followed by one of <code>'"'</code> or <code>"'"</code>, followed by a space. One problem with this algorithm is that it is unable to detect the difference between “Dr.” in</p> <pre data-language="python">[...] Dr. Frankenstein's monster [...]
</pre> <p>and “Spot.” in</p> <pre data-language="python">[...] See Spot. See Spot run [...]
</pre> <p><a class="reference internal" href="#textwrap.TextWrapper.fix_sentence_endings" title="textwrap.TextWrapper.fix_sentence_endings"><code>fix_sentence_endings</code></a> is false by default.</p> <p>Since the sentence detection algorithm relies on <code>string.lowercase</code> for the definition of “lowercase letter”, and a convention of using two spaces after a period to separate sentences on the same line, it is specific to English-language texts.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.break_long_words">
<code>break_long_words</code> </dt> <dd>
<p>(default: <code>True</code>) If true, then words longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code>width</code></a> will be broken in order to ensure that no lines are longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code>width</code></a>. If it is false, long words will not be broken, and some lines may be longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code>width</code></a>. (Long words will be put on a line by themselves, in order to minimize the amount by which <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code>width</code></a> is exceeded.)</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.break_on_hyphens">
<code>break_on_hyphens</code> </dt> <dd>
<p>(default: <code>True</code>) If true, wrapping will occur preferably on whitespaces and right after hyphens in compound words, as it is customary in English. If false, only whitespaces will be considered as potentially good places 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>break_long_words</code></a> to false if you want truly insecable words. Default behaviour in previous versions was to always allow breaking hyphenated words.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.max_lines">
<code>max_lines</code> </dt> <dd>
<p>(default: <code>None</code>) If not <code>None</code>, then the output will contain at most <em>max_lines</em> lines, with <em>placeholder</em> appearing at the end of the output.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
</dl> <span class="target" id="index-0"></span><dl class="py attribute"> <dt class="sig sig-object py" id="textwrap.TextWrapper.placeholder">
<code>placeholder</code> </dt> <dd>
<p>(default: <code>' [...]'</code>) String that will appear at the end of the output text if it has been truncated.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
</dl> <p><a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> also provides some public methods, analogous to the module-level convenience functions:</p> <dl class="py method"> <dt class="sig sig-object py" id="textwrap.TextWrapper.wrap">
<code>wrap(text)</code> </dt> <dd>
<p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code>width</code></a> characters long. All wrapping options are taken from instance attributes of the <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code>TextWrapper</code></a> instance. Returns a list of output lines, without final newlines. If the wrapped output has no content, the returned list is empty.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="textwrap.TextWrapper.fill">
<code>fill(text)</code> </dt> <dd>
<p>Wraps the single paragraph in <em>text</em>, and returns a single string containing the wrapped paragraph.</p> </dd>
</dl> </dd>
</dl> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/textwrap.html" class="_attribution-link">https://docs.python.org/3.12/library/textwrap.html</a>
</p>
</div>
|