<str> = <str>.strip() # Strips all whitespace characters from both ends.
<str> = <str>.strip('<chars>') # Strips all passed characters from both ends.
<str> = <str>.strip('<chars>') # Strips passed characters. Also lstrip/rstrip().
```
```python
@ -321,6 +323,7 @@ String
```
```python
<str> = <str>.lower() # Changes the case. Also upper/capitalize/title().
<str> = <str>.replace(old, new [, count]) # Replaces 'old' with 'new' at most 'count' times.
<str> = <str>.translate(<table>) # Use `str.maketrans(<dict>)` to generate table.
```
@ -329,38 +332,37 @@ String
<str> = chr(<int>) # Converts int to Unicode character.
<int> = ord(<str>) # Converts Unicode character to int.
```
* **Also: `'lstrip()'`, `'rstrip()'` and `'rsplit()'`.**
* **Also: `'lower()'`, `'upper()'`, `'capitalize()'` and `'title()'`.**
* **Use `'unicodedata.normalize("NFC", <str>)'` on strings that may contain characters like `'Ö'` before comparing them, because they can be stored as one or two characters.**
<div><h2id="string"><ahref="#string"name="string">#</a>String</h2><pre><codeclass="python language-python hljs"><str> = <str>.strip() <spanclass="hljs-comment"># Strips all whitespace characters from both ends.</span>
<str> = <str>.strip(<spanclass="hljs-string">'<chars>'</span>) <spanclass="hljs-comment"># Strips all passed characters from both ends.</span>
<div><h2id="string"><ahref="#string"name="string">#</a>String</h2><p><strong>Immutable sequence of characters.</strong></p><pre><codeclass="python language-python hljs"><str> = <str>.strip() <spanclass="hljs-comment"># Strips all whitespace characters from both ends.</span>
<str> = <str>.strip(<spanclass="hljs-string">'<chars>'</span>) <spanclass="hljs-comment"># Strips passed characters. Also lstrip/rstrip().</span>
</code></pre></div>
<pre><codeclass="python language-python hljs"><list> = <str>.split() <spanclass="hljs-comment"># Splits on one or more whitespace characters.</span>
<list> = <str>.split(sep=<spanclass="hljs-keyword">None</span>, maxsplit=<spanclass="hljs-number">-1</span>) <spanclass="hljs-comment"># Splits on 'sep' str at most 'maxsplit' times.</span>
<list> = <str>.splitlines(keepends=<spanclass="hljs-keyword">False</span>) <spanclass="hljs-comment"># On [\n\r\f\v\x1c-\x1e\x85\u2028\u2029] and \r\n.</span>
<int> = <str>.find(<sub_str>) <spanclass="hljs-comment"># Returns start index of the first match or -1.</span>
<int> = <str>.index(<sub_str>) <spanclass="hljs-comment"># Same, but raises ValueError if missing.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><str> = <str>.replace(old, new [, count]) <spanclass="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
<pre><codeclass="python language-python hljs"><str> = <str>.lower() <spanclass="hljs-comment"># Changes the case. Also upper/capitalize/title().</span>
<str> = <str>.replace(old, new [, count]) <spanclass="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
<str> = <str>.translate(<table>) <spanclass="hljs-comment"># Use `str.maketrans(<dict>)` to generate table.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><str> = chr(<int>) <spanclass="hljs-comment"># Converts int to Unicode character.</span>
<int> = ord(<str>) <spanclass="hljs-comment"># Converts Unicode character to int.</span>
</code></pre>
<ul>
<li><strong>Also: <codeclass="python hljs"><spanclass="hljs-string">'lstrip()'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'rstrip()'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'rsplit()'</span></code>.</strong></li>
<li><strong>Also: <codeclass="python hljs"><spanclass="hljs-string">'lower()'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'upper()'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'capitalize()'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'title()'</span></code>.</strong></li>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'unicodedata.normalize("NFC", <str>)'</span></code> on strings that may contain characters like <codeclass="python hljs"><spanclass="hljs-string">'Ö'</span></code> before comparing them, because they can be stored as one or two characters.</strong></li>
<div><h3id="propertymethods">Property Methods</h3><pre><codeclass="python language-python hljs"><bool> = <str>.isdecimal() <spanclass="hljs-comment"># Checks for [0-9].</span>
<bool> = <str>.isdigit() <spanclass="hljs-comment"># Checks for [²³¹] and isdecimal().</span>
<bool> = <str>.isnumeric() <spanclass="hljs-comment"># Checks for [¼½¾] and isdigit().</span>
<bool> = <str>.isalnum() <spanclass="hljs-comment"># Checks for [a-zA-Z] and isnumeric().</span>
<bool> = <str>.isprintable() <spanclass="hljs-comment"># Checks for [ !#$%…] and isalnum().</span>
<bool> = <str>.isspace() <spanclass="hljs-comment"># Checks for [ \t\n\r\f\v\x1c-\x1f\x85\xa0…].</span>
</code></pre></div>
<ul>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'isspace()'</span></code> checks for whitespaces: <codeclass="python hljs"><spanclass="hljs-string">'[ \t\n\r\f\v\x1c-\x1f\x85\xa0\u1680…]'</span></code>.</strong></li>
</ul>
<div><h2id="regex"><ahref="#regex"name="regex">#</a>Regex</h2><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> re
<str> = re.sub(<regex>, new, text, count=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Substitutes all occurrences with 'new'.</span>
<div><h2id="regex"><ahref="#regex"name="regex">#</a>Regex</h2><p><strong>Functions for regular expression matching.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> re
</code></pre></div>
<pre><codeclass="python language-python hljs"><str> = re.sub(<regex>, new, text, count=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Substitutes all occurrences with 'new'.</span>
<list> = re.findall(<regex>, text) <spanclass="hljs-comment"># Returns all occurrences as strings.</span>
<list> = re.split(<regex>, text, maxsplit=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Add brackets around regex to include matches.</span>
<Match> = re.search(<regex>, text) <spanclass="hljs-comment"># Searches for first occurrence of the pattern.</span>
<Match> = re.search(<regex>, text) <spanclass="hljs-comment"># First occurrence of the pattern or None.</span>
<Match> = re.match(<regex>, text) <spanclass="hljs-comment"># Searches only at the beginning of the text.</span>
<iter> = re.finditer(<regex>, text) <spanclass="hljs-comment"># Returns all occurrences as Match objects.</span>
</code></pre></div>
</code></pre>
<ul>
<li><strong>Argument 'new' can be a function that accepts a Match object and returns a string.</strong></li>
<li><strong>Search() and match() return None if they can't find a match.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.IGNORECASE'</span></code> can be used with all functions.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.MULTILINE'</span></code> makes <codeclass="python hljs"><spanclass="hljs-string">'^'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'$'</span></code> match the start/end of each line.</strong></li>
<li><strong>Argument <codeclass="python hljs"><spanclass="hljs-string">'flags=re.DOTALL'</span></code> makes <codeclass="python hljs"><spanclass="hljs-string">'.'</span></code> also accept the <codeclass="python hljs"><spanclass="hljs-string">'\n'</span></code>.</strong></li>