parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="textwrap — Text wrapping and filling" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/textwrap.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/textwrap.py The textwrap module provides some convenience functions, as well as TextWrapper, the class that does all the work. If you’re just wrapping or filling one or two text st..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_textwrap_435f9ce8.png" />15<meta property="og:image:alt" content="Source code: Lib/textwrap.py The textwrap module provides some convenience functions, as well as TextWrapper, the class that does all the work. If you’re just wrapping or filling one or two text st..." />16<meta name="description" content="Source code: Lib/textwrap.py The textwrap module provides some convenience functions, as well as TextWrapper, the class that does all the work. If you’re just wrapping or filling one or two text st..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>textwrap — Text wrapping and filling — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="unicodedata — Unicode Database" href="unicodedata.html" />43 <link rel="prev" title="difflib — Helpers for computing deltas" href="difflib.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/textwrap.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h4>Previous topic</h4>106 <p class="topless"><a href="difflib.html"107 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code> — Helpers for computing deltas</a></p>108 </div>109 <div>110 <h4>Next topic</h4>111 <p class="topless"><a href="unicodedata.html"112 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code> — Unicode Database</a></p>113 </div>114 <script>115 document.addEventListener('DOMContentLoaded', () => {116 const title = document.querySelector('meta[property="og:title"]').content;117 const elements = document.querySelectorAll('.improvepage');118 const pageurl = window.location.href.split('?')[0];119 elements.forEach(element => {120 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));121 url.searchParams.set('pagetitle', title);122 url.searchParams.set('pageurl', pageurl);123 url.searchParams.set('pagesource', "library/textwrap.rst");124 element.href = url.toString();125 });126 });127 </script>128 <div role="note" aria-label="source link">129 <h3>This page</h3>130 <ul class="this-page-menu">131 <li><a href="../bugs.html">Report a bug</a></li>132 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>133 <li>134 <a href="https://github.com/python/cpython/blob/main/Doc/library/textwrap.rst?plain=1"135 rel="nofollow">Show source136 </a>137 </li>138 139 </ul>140 </div>141 </nav>142 </div>143</div>144 145 146 <div class="related" role="navigation" aria-label="Related">147 <h3>Navigation</h3>148 <ul>149 <li class="right" style="margin-right: 10px">150 <a href="../genindex.html" title="General Index"151 accesskey="I">index</a></li>152 <li class="right" >153 <a href="../py-modindex.html" title="Python Module Index"154 >modules</a> |</li>155 <li class="right" >156 <a href="unicodedata.html" title="unicodedata — Unicode Database"157 accesskey="N">next</a> |</li>158 <li class="right" >159 <a href="difflib.html" title="difflib — Helpers for computing deltas"160 accesskey="P">previous</a> |</li>161 162 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>163 <li><a href="https://www.python.org/">Python</a> »</li>164 <li class="switchers">165 <div class="language_switcher_placeholder"></div>166 <div class="version_switcher_placeholder"></div>167 </li>168 <li>169 170 </li>171 <li id="cpython-language-and-version">172 <a href="../index.html">3.15.0a6 Documentation</a> »173 </li>174 175 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>176 <li class="nav-item nav-item-2"><a href="text.html" accesskey="U">Text Processing Services</a> »</li>177 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">textwrap</span></code> — Text wrapping and filling</a></li>178 <li class="right">179 180 181 <div class="inline-search" role="search">182 <form class="inline-search" action="../search.html" method="get">183 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">184 <input type="submit" value="Go">185 </form>186 </div>187 |188 </li>189 <li class="right">190<label class="theme-selector-label">191 Theme192 <select class="theme-selector" oninput="activateTheme(this.value)">193 <option value="auto" selected>Auto</option>194 <option value="light">Light</option>195 <option value="dark">Dark</option>196 </select>197</label> |</li>198 199 </ul>200 </div> 201 202 <div class="document">203 <div class="documentwrapper">204 <div class="bodywrapper">205 <div class="body" role="main">206 207 <section id="module-textwrap">208<span id="textwrap-text-wrapping-and-filling"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">textwrap</span></code> — Text wrapping and filling<a class="headerlink" href="#module-textwrap" title="Link to this heading">¶</a></h1>209<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/textwrap.py">Lib/textwrap.py</a></p>210<hr class="docutils" />211<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">textwrap</span></code> module provides some convenience functions,212as well as <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code></a>, the class that does all the work.213If you’re just wrapping or filling one or two text strings, the convenience214functions should be good enough; otherwise, you should use an instance of215<code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> for efficiency.</p>216<dl class="py function">217<dt class="sig sig-object py" id="textwrap.wrap">218<span class="sig-prename descclassname"><span class="pre">textwrap.</span></span><span class="sig-name descname"><span class="pre">wrap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">width</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">70</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">initial_indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subsequent_indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expand_tabs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">replace_whitespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fix_sentence_endings</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">break_long_words</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">drop_whitespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">break_on_hyphens</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tabsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">8</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_lines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">placeholder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'</span> <span class="pre">[...]'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.wrap" title="Link to this definition">¶</a></dt>219<dd><p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most220<em>width</em> characters long. Returns a list of output lines, without final221newlines.</p>222<p>Optional keyword arguments correspond to the instance attributes of223<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code></a>, documented below.</p>224<p>See the <a class="reference internal" href="#textwrap.TextWrapper.wrap" title="textwrap.TextWrapper.wrap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TextWrapper.wrap()</span></code></a> method for additional details on how225<code class="xref py py-func docutils literal notranslate"><span class="pre">wrap()</span></code> behaves.</p>226</dd></dl>227 228<dl class="py function">229<dt class="sig sig-object py" id="textwrap.fill">230<span class="sig-prename descclassname"><span class="pre">textwrap.</span></span><span class="sig-name descname"><span class="pre">fill</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">width</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">70</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">initial_indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subsequent_indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">expand_tabs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">replace_whitespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fix_sentence_endings</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">break_long_words</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">drop_whitespace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">break_on_hyphens</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tabsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">8</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_lines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">placeholder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'</span> <span class="pre">[...]'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.fill" title="Link to this definition">¶</a></dt>231<dd><p>Wraps the single paragraph in <em>text</em>, and returns a single string containing the232wrapped paragraph. <code class="xref py py-func docutils literal notranslate"><span class="pre">fill()</span></code> is shorthand for</p>233<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span>234</pre></div>235</div>236<p>In particular, <code class="xref py py-func docutils literal notranslate"><span class="pre">fill()</span></code> accepts exactly the same keyword arguments as237<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal notranslate"><span class="pre">wrap()</span></code></a>.</p>238</dd></dl>239 240<dl class="py function">241<dt class="sig sig-object py" id="textwrap.shorten">242<span class="sig-prename descclassname"><span class="pre">textwrap.</span></span><span class="sig-name descname"><span class="pre">shorten</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">width</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">fix_sentence_endings</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">break_long_words</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">break_on_hyphens</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">placeholder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'</span> <span class="pre">[...]'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.shorten" title="Link to this definition">¶</a></dt>243<dd><p>Collapse and truncate the given <em>text</em> to fit in the given <em>width</em>.</p>244<p>First the whitespace in <em>text</em> is collapsed (all whitespace is replaced by245single spaces). If the result fits in the <em>width</em>, it is returned.246Otherwise, enough words are dropped from the end so that the remaining words247plus the <em>placeholder</em> fit within <em>width</em>:</p>248<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s2">"Hello world!"</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>249<span class="go">'Hello world!'</span>250<span class="gp">>>> </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s2">"Hello world!"</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span>251<span class="go">'Hello [...]'</span>252<span class="gp">>>> </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s2">"Hello world"</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">placeholder</span><span class="o">=</span><span class="s2">"..."</span><span class="p">)</span>253<span class="go">'Hello...'</span>254</pre></div>255</div>256<p>Optional keyword arguments correspond to the instance attributes of257<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code></a>, documented below. Note that the whitespace is258collapsed before the text is passed to the <code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fill()</span></code></a>259function, so changing the value of <a class="reference internal" href="#textwrap.TextWrapper.tabsize" title="textwrap.TextWrapper.tabsize"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tabsize</span></code></a>, <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">expand_tabs</span></code></a>,260<a class="reference internal" href="#textwrap.TextWrapper.drop_whitespace" title="textwrap.TextWrapper.drop_whitespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">drop_whitespace</span></code></a>, and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">replace_whitespace</span></code></a> will have no effect.</p>261<div class="versionadded">262<p><span class="versionmodified added">Added in version 3.4.</span></p>263</div>264</dd></dl>265 266<dl class="py function">267<dt class="sig sig-object py" id="textwrap.dedent">268<span class="sig-prename descclassname"><span class="pre">textwrap.</span></span><span class="sig-name descname"><span class="pre">dedent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.dedent" title="Link to this definition">¶</a></dt>269<dd><p>Remove any common leading whitespace from every line in <em>text</em>.</p>270<p>This can be used to make triple-quoted strings line up with the left edge of the271display, while still presenting them in the source code in indented form.</p>272<p>Note that tabs and spaces are both treated as whitespace, but they are not273equal: the lines <code class="docutils literal notranslate"><span class="pre">"</span>  <span class="pre">hello"</span></code> and <code class="docutils literal notranslate"><span class="pre">"\thello"</span></code> are considered to have no274common leading whitespace.</p>275<p>Lines containing only whitespace are ignored in the input and normalized to a276single newline character in the output.</p>277<p>For example:</p>278<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">test</span><span class="p">():</span>279 <span class="c1"># end first line with \ to avoid the empty line!</span>280 <span class="n">s</span> <span class="o">=</span> <span class="s1">'''</span><span class="se">\</span>281<span class="s1"> hello</span>282<span class="s1"> world</span>283<span class="s1"> '''</span>284 <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="c1"># prints ' hello\n world\n '</span>285 <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">dedent</span><span class="p">(</span><span class="n">s</span><span class="p">)))</span> <span class="c1"># prints 'hello\n world\n'</span>286</pre></div>287</div>288<div class="versionchanged">289<p><span class="versionmodified changed">Changed in version 3.14: </span>The <code class="xref py py-func docutils literal notranslate"><span class="pre">dedent()</span></code> function now correctly normalizes blank lines containing290only whitespace characters. Previously, the implementation only normalized291blank lines containing tabs and spaces.</p>292</div>293</dd></dl>294 295<dl class="py function">296<dt class="sig sig-object py" id="textwrap.indent">297<span class="sig-prename descclassname"><span class="pre">textwrap.</span></span><span class="sig-name descname"><span class="pre">indent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">predicate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.indent" title="Link to this definition">¶</a></dt>298<dd><p>Add <em>prefix</em> to the beginning of selected lines in <em>text</em>.</p>299<p>Lines are separated by calling <code class="docutils literal notranslate"><span class="pre">text.splitlines(True)</span></code>.</p>300<p>By default, <em>prefix</em> is added to all lines that do not consist301solely of whitespace (including any line endings).</p>302<p>For example:</p>303<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s1">'hello</span><span class="se">\n\n</span><span class="s1"> </span><span class="se">\n</span><span class="s1">world'</span>304<span class="gp">>>> </span><span class="n">indent</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s1">' '</span><span class="p">)</span>305<span class="go">' hello\n\n \n world'</span>306</pre></div>307</div>308<p>The optional <em>predicate</em> argument can be used to control which lines309are indented. For example, it is easy to add <em>prefix</em> to even empty310and whitespace-only lines:</p>311<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">indent</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s1">'+ '</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">line</span><span class="p">:</span> <span class="kc">True</span><span class="p">))</span>312<span class="go">+ hello</span>313<span class="go">+</span>314<span class="go">+</span>315<span class="go">+ world</span>316</pre></div>317</div>318<div class="versionadded">319<p><span class="versionmodified added">Added in version 3.3.</span></p>320</div>321</dd></dl>322 323<p><a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-func docutils literal notranslate"><span class="pre">wrap()</span></code></a>, <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><code class="xref py py-func docutils literal notranslate"><span class="pre">fill()</span></code></a> and <a class="reference internal" href="#textwrap.shorten" title="textwrap.shorten"><code class="xref py py-func docutils literal notranslate"><span class="pre">shorten()</span></code></a> work by creating a324<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code></a> instance and calling a single method on it. That325instance is not reused, so for applications that process many text326strings using <code class="xref py py-func docutils literal notranslate"><span class="pre">wrap()</span></code> and/or <code class="xref py py-func docutils literal notranslate"><span class="pre">fill()</span></code>, it may be more efficient to327create your own <code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> object.</p>328<p>Text is preferably wrapped on whitespaces and right after the hyphens in329hyphenated words; only then will long words be broken if necessary, unless330<a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TextWrapper.break_long_words</span></code></a> is set to false.</p>331<dl class="py class">332<dt class="sig sig-object py" id="textwrap.TextWrapper">333<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">textwrap.</span></span><span class="sig-name descname"><span class="pre">TextWrapper</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.TextWrapper" title="Link to this definition">¶</a></dt>334<dd><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> constructor accepts a number of optional keyword335arguments. Each keyword argument corresponds to an instance attribute, so336for example</p>337<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">(</span><span class="n">initial_indent</span><span class="o">=</span><span class="s2">"* "</span><span class="p">)</span>338</pre></div>339</div>340<p>is the same as</p>341<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">()</span>342<span class="n">wrapper</span><span class="o">.</span><span class="n">initial_indent</span> <span class="o">=</span> <span class="s2">"* "</span>343</pre></div>344</div>345<p>You can reuse the same <code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> object many times, and you can346change any of its options through direct assignment to instance attributes347between uses.</p>348<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> instance attributes (and keyword arguments to the349constructor) are as follows:</p>350<dl class="py attribute">351<dt class="sig sig-object py" id="textwrap.TextWrapper.width">352<span class="sig-name descname"><span class="pre">width</span></span><a class="headerlink" href="#textwrap.TextWrapper.width" title="Link to this definition">¶</a></dt>353<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">70</span></code>) The maximum length of wrapped lines. As long as there354are no individual words in the input text longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal notranslate"><span class="pre">width</span></code></a>,355<code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> guarantees that no output line will be longer than356<code class="xref py py-attr docutils literal notranslate"><span class="pre">width</span></code> characters.</p>357</dd></dl>358 359<dl class="py attribute">360<dt class="sig sig-object py" id="textwrap.TextWrapper.expand_tabs">361<span class="sig-name descname"><span class="pre">expand_tabs</span></span><a class="headerlink" href="#textwrap.TextWrapper.expand_tabs" title="Link to this definition">¶</a></dt>362<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">True</span></code>) If true, then all tab characters in <em>text</em> will be363expanded to spaces using the <a class="reference internal" href="stdtypes.html#str.expandtabs" title="str.expandtabs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">expandtabs()</span></code></a> method of <em>text</em>.</p>364</dd></dl>365 366<dl class="py attribute">367<dt class="sig sig-object py" id="textwrap.TextWrapper.tabsize">368<span class="sig-name descname"><span class="pre">tabsize</span></span><a class="headerlink" href="#textwrap.TextWrapper.tabsize" title="Link to this definition">¶</a></dt>369<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">8</span></code>) If <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">expand_tabs</span></code></a> is true, then all tab characters370in <em>text</em> will be expanded to zero or more spaces, depending on the371current column and the given tab size.</p>372<div class="versionadded">373<p><span class="versionmodified added">Added in version 3.3.</span></p>374</div>375</dd></dl>376 377<dl class="py attribute">378<dt class="sig sig-object py" id="textwrap.TextWrapper.replace_whitespace">379<span class="sig-name descname"><span class="pre">replace_whitespace</span></span><a class="headerlink" href="#textwrap.TextWrapper.replace_whitespace" title="Link to this definition">¶</a></dt>380<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">True</span></code>) If true, after tab expansion but before wrapping,381the <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wrap()</span></code></a> method will replace each whitespace character382with a single space. The whitespace characters replaced are383as follows: tab, newline, vertical tab, formfeed, and carriage384return (<code class="docutils literal notranslate"><span class="pre">'\t\n\v\f\r'</span></code>).</p>385<div class="admonition note">386<p class="admonition-title">Note</p>387<p>If <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">expand_tabs</span></code></a> is false and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">replace_whitespace</span></code></a> is true,388each tab character will be replaced by a single space, which is <em>not</em>389the same as tab expansion.</p>390</div>391<div class="admonition note">392<p class="admonition-title">Note</p>393<p>If <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">replace_whitespace</span></code></a> is false, newlines may appear in the394middle of a line and cause strange output. For this reason, text should395be split into paragraphs (using <a class="reference internal" href="stdtypes.html#str.splitlines" title="str.splitlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.splitlines()</span></code></a> or similar)396which are wrapped separately.</p>397</div>398</dd></dl>399 400<dl class="py attribute">401<dt class="sig sig-object py" id="textwrap.TextWrapper.drop_whitespace">402<span class="sig-name descname"><span class="pre">drop_whitespace</span></span><a class="headerlink" href="#textwrap.TextWrapper.drop_whitespace" title="Link to this definition">¶</a></dt>403<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">True</span></code>) If true, whitespace at the beginning and ending of404every line (after wrapping but before indenting) is dropped.405Whitespace at the beginning of the paragraph, however, is not dropped406if non-whitespace follows it. If whitespace being dropped takes up an407entire line, the whole line is dropped.</p>408</dd></dl>409 410<dl class="py attribute">411<dt class="sig sig-object py" id="textwrap.TextWrapper.initial_indent">412<span class="sig-name descname"><span class="pre">initial_indent</span></span><a class="headerlink" href="#textwrap.TextWrapper.initial_indent" title="Link to this definition">¶</a></dt>413<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">''</span></code>) String that will be prepended to the first line of414wrapped output. Counts towards the length of the first line. The empty415string is not indented.</p>416</dd></dl>417 418<dl class="py attribute">419<dt class="sig sig-object py" id="textwrap.TextWrapper.subsequent_indent">420<span class="sig-name descname"><span class="pre">subsequent_indent</span></span><a class="headerlink" href="#textwrap.TextWrapper.subsequent_indent" title="Link to this definition">¶</a></dt>421<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">''</span></code>) String that will be prepended to all lines of wrapped422output except the first. Counts towards the length of each line except423the first.</p>424</dd></dl>425 426<dl class="py attribute">427<dt class="sig sig-object py" id="textwrap.TextWrapper.fix_sentence_endings">428<span class="sig-name descname"><span class="pre">fix_sentence_endings</span></span><a class="headerlink" href="#textwrap.TextWrapper.fix_sentence_endings" title="Link to this definition">¶</a></dt>429<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">False</span></code>) If true, <code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> attempts to detect430sentence endings and ensure that sentences are always separated by exactly431two spaces. This is generally desired for text in a monospaced font.432However, the sentence detection algorithm is imperfect: it assumes that a433sentence ending consists of a lowercase letter followed by one of <code class="docutils literal notranslate"><span class="pre">'.'</span></code>,434<code class="docutils literal notranslate"><span class="pre">'!'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'?'</span></code>, possibly followed by one of <code class="docutils literal notranslate"><span class="pre">'"'</span></code> or <code class="docutils literal notranslate"><span class="pre">"'"</span></code>,435followed by a space. One problem with this algorithm is that it is436unable to detect the difference between “Dr.” in</p>437<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">Dr</span><span class="o">.</span> <span class="n">Frankenstein</span><span class="s1">'s monster [...]</span>438</pre></div>439</div>440<p>and “Spot.” in</p>441<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">See</span> <span class="n">Spot</span><span class="o">.</span> <span class="n">See</span> <span class="n">Spot</span> <span class="n">run</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span>442</pre></div>443</div>444<p><a class="reference internal" href="#textwrap.TextWrapper.fix_sentence_endings" title="textwrap.TextWrapper.fix_sentence_endings"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fix_sentence_endings</span></code></a> is false by default.</p>445<p>Since the sentence detection algorithm relies on <code class="docutils literal notranslate"><span class="pre">string.lowercase</span></code> for446the definition of “lowercase letter”, and a convention of using two spaces447after a period to separate sentences on the same line, it is specific to448English-language texts.</p>449</dd></dl>450 451<dl class="py attribute">452<dt class="sig sig-object py" id="textwrap.TextWrapper.break_long_words">453<span class="sig-name descname"><span class="pre">break_long_words</span></span><a class="headerlink" href="#textwrap.TextWrapper.break_long_words" title="Link to this definition">¶</a></dt>454<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">True</span></code>) If true, then words longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal notranslate"><span class="pre">width</span></code></a> will be455broken in order to ensure that no lines are longer than <code class="xref py py-attr docutils literal notranslate"><span class="pre">width</span></code>. If456it is false, long words will not be broken, and some lines may be longer457than <code class="xref py py-attr docutils literal notranslate"><span class="pre">width</span></code>. (Long words will be put on a line by themselves, in458order to minimize the amount by which <code class="xref py py-attr docutils literal notranslate"><span class="pre">width</span></code> is exceeded.)</p>459</dd></dl>460 461<dl class="py attribute">462<dt class="sig sig-object py" id="textwrap.TextWrapper.break_on_hyphens">463<span class="sig-name descname"><span class="pre">break_on_hyphens</span></span><a class="headerlink" href="#textwrap.TextWrapper.break_on_hyphens" title="Link to this definition">¶</a></dt>464<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">True</span></code>) If true, wrapping will occur preferably on whitespaces465and right after hyphens in compound words, as it is customary in English.466If false, only whitespaces will be considered as potentially good places467for line breaks, but you need to set <a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><code class="xref py py-attr docutils literal notranslate"><span class="pre">break_long_words</span></code></a> to false if468you want truly insecable words. Default behaviour in previous versions469was to always allow breaking hyphenated words.</p>470</dd></dl>471 472<dl class="py attribute">473<dt class="sig sig-object py" id="textwrap.TextWrapper.max_lines">474<span class="sig-name descname"><span class="pre">max_lines</span></span><a class="headerlink" href="#textwrap.TextWrapper.max_lines" title="Link to this definition">¶</a></dt>475<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">None</span></code>) If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the output will contain at most476<em>max_lines</em> lines, with <em>placeholder</em> appearing at the end of the output.</p>477<div class="versionadded">478<p><span class="versionmodified added">Added in version 3.4.</span></p>479</div>480</dd></dl>481 482<dl class="py attribute" id="index-0">483<dt class="sig sig-object py" id="textwrap.TextWrapper.placeholder">484<span class="sig-name descname"><span class="pre">placeholder</span></span><a class="headerlink" href="#textwrap.TextWrapper.placeholder" title="Link to this definition">¶</a></dt>485<dd><p>(default: <code class="docutils literal notranslate"><span class="pre">'</span> <span class="pre">[...]'</span></code>) String that will appear at the end of the output486text if it has been truncated.</p>487<div class="versionadded">488<p><span class="versionmodified added">Added in version 3.4.</span></p>489</div>490</dd></dl>491 492<p><code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> also provides some public methods, analogous to the493module-level convenience functions:</p>494<dl class="py method">495<dt class="sig sig-object py" id="textwrap.TextWrapper.wrap">496<span class="sig-name descname"><span class="pre">wrap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.TextWrapper.wrap" title="Link to this definition">¶</a></dt>497<dd><p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most498<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><code class="xref py py-attr docutils literal notranslate"><span class="pre">width</span></code></a> characters long. All wrapping options are taken from499instance attributes of the <code class="xref py py-class docutils literal notranslate"><span class="pre">TextWrapper</span></code> instance. Returns a list500of output lines, without final newlines. If the wrapped output has no501content, the returned list is empty.</p>502</dd></dl>503 504<dl class="py method">505<dt class="sig sig-object py" id="textwrap.TextWrapper.fill">506<span class="sig-name descname"><span class="pre">fill</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#textwrap.TextWrapper.fill" title="Link to this definition">¶</a></dt>507<dd><p>Wraps the single paragraph in <em>text</em>, and returns a single string508containing the wrapped paragraph.</p>509</dd></dl>510 511</dd></dl>512 513</section>514 515 516 <div class="clearer"></div>517 </div>518 </div>519 </div>520 <div class="sphinxsidebar" role="navigation" aria-label="Main">521 <div class="sphinxsidebarwrapper">522 <div>523 <h4>Previous topic</h4>524 <p class="topless"><a href="difflib.html"525 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code> — Helpers for computing deltas</a></p>526 </div>527 <div>528 <h4>Next topic</h4>529 <p class="topless"><a href="unicodedata.html"530 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code> — Unicode Database</a></p>531 </div>532 <script>533 document.addEventListener('DOMContentLoaded', () => {534 const title = document.querySelector('meta[property="og:title"]').content;535 const elements = document.querySelectorAll('.improvepage');536 const pageurl = window.location.href.split('?')[0];537 elements.forEach(element => {538 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));539 url.searchParams.set('pagetitle', title);540 url.searchParams.set('pageurl', pageurl);541 url.searchParams.set('pagesource', "library/textwrap.rst");542 element.href = url.toString();543 });544 });545 </script>546 <div role="note" aria-label="source link">547 <h3>This page</h3>548 <ul class="this-page-menu">549 <li><a href="../bugs.html">Report a bug</a></li>550 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>551 <li>552 <a href="https://github.com/python/cpython/blob/main/Doc/library/textwrap.rst?plain=1"553 rel="nofollow">Show source554 </a>555 </li>556 557 </ul>558 </div>559 </div>560<div id="sidebarbutton" title="Collapse sidebar">561<span>«</span>562</div>563 564 </div>565 <div class="clearer"></div>566 </div> 567 <div class="related" role="navigation" aria-label="Related">568 <h3>Navigation</h3>569 <ul>570 <li class="right" style="margin-right: 10px">571 <a href="../genindex.html" title="General Index"572 >index</a></li>573 <li class="right" >574 <a href="../py-modindex.html" title="Python Module Index"575 >modules</a> |</li>576 <li class="right" >577 <a href="unicodedata.html" title="unicodedata — Unicode Database"578 >next</a> |</li>579 <li class="right" >580 <a href="difflib.html" title="difflib — Helpers for computing deltas"581 >previous</a> |</li>582 583 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>584 <li><a href="https://www.python.org/">Python</a> »</li>585 <li class="switchers">586 <div class="language_switcher_placeholder"></div>587 <div class="version_switcher_placeholder"></div>588 </li>589 <li>590 591 </li>592 <li id="cpython-language-and-version">593 <a href="../index.html">3.15.0a6 Documentation</a> »594 </li>595 596 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>597 <li class="nav-item nav-item-2"><a href="text.html" >Text Processing Services</a> »</li>598 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">textwrap</span></code> — Text wrapping and filling</a></li>599 <li class="right">600 601 602 <div class="inline-search" role="search">603 <form class="inline-search" action="../search.html" method="get">604 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">605 <input type="submit" value="Go">606 </form>607 </div>608 |609 </li>610 <li class="right">611<label class="theme-selector-label">612 Theme613 <select class="theme-selector" oninput="activateTheme(this.value)">614 <option value="auto" selected>Auto</option>615 <option value="light">Light</option>616 <option value="dark">Dark</option>617 </select>618</label> |</li>619 620 </ul>621 </div> 622 <div class="footer">623 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.624 <br>625 This page is licensed under the Python Software Foundation License Version 2.626 <br>627 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.628 <br>629 630 See <a href="/license.html">History and License</a> for more information.<br>631 632 633 <br>634 635 The Python Software Foundation is a non-profit corporation.636<a href="https://www.python.org/psf/donations/">Please donate.</a>637<br>638 <br>639 Last updated on Mar 10, 2026 (08:58 UTC).640 641 <a href="/bugs.html">Found a bug</a>?642 643 <br>644 645 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.646 </div>647 648 </body>649</html>