|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>October 4, 2023</aside> |
| 57 | + <aside>October 11, 2023</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
290 | 290 | ┃ decimal.Decimal │ ✓ │ │ │ │ ┃
|
291 | 291 | ┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛
|
292 | 292 | </code></pre>
|
293 |
| -<div><h2 id="string"><a href="#string" name="string">#</a>String</h2><pre><code class="python language-python hljs"><str> = <str>.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span> |
294 |
| -<str> = <str>.strip(<span class="hljs-string">'<chars>'</span>) <span class="hljs-comment"># Strips all passed characters from both ends.</span> |
| 293 | +<div><h2 id="string"><a href="#string" name="string">#</a>String</h2><p><strong>Immutable sequence of characters.</strong></p><pre><code class="python language-python hljs"><str> = <str>.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span> |
| 294 | +<str> = <str>.strip(<span class="hljs-string">'<chars>'</span>) <span class="hljs-comment"># Strips passed characters. Also lstrip/rstrip().</span> |
295 | 295 | </code></pre></div>
|
296 | 296 |
|
| 297 | + |
297 | 298 | <pre><code class="python language-python hljs"><list> = <str>.split() <span class="hljs-comment"># Splits on one or more whitespace characters.</span>
|
298 | 299 | <list> = <str>.split(sep=<span class="hljs-keyword">None</span>, maxsplit=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Splits on 'sep' str at most 'maxsplit' times.</span>
|
299 | 300 | <list> = <str>.splitlines(keepends=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># On [\n\r\f\v\x1c-\x1e\x85\u2028\u2029] and \r\n.</span>
|
|
305 | 306 | <int> = <str>.find(<sub_str>) <span class="hljs-comment"># Returns start index of the first match or -1.</span>
|
306 | 307 | <int> = <str>.index(<sub_str>) <span class="hljs-comment"># Same, but raises ValueError if missing.</span>
|
307 | 308 | </code></pre>
|
308 |
| -<pre><code class="python language-python hljs"><str> = <str>.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span> |
| 309 | +<pre><code class="python language-python hljs"><str> = <str>.lower() <span class="hljs-comment"># Changes the case. Also upper/capitalize/title().</span> |
| 310 | +<str> = <str>.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span> |
309 | 311 | <str> = <str>.translate(<table>) <span class="hljs-comment"># Use `str.maketrans(<dict>)` to generate table.</span>
|
310 | 312 | </code></pre>
|
311 | 313 | <pre><code class="python language-python hljs"><str> = chr(<int>) <span class="hljs-comment"># Converts int to Unicode character.</span>
|
312 | 314 | <int> = ord(<str>) <span class="hljs-comment"># Converts Unicode character to int.</span>
|
313 | 315 | </code></pre>
|
314 | 316 | <ul>
|
315 |
| -<li><strong>Also: <code class="python hljs"><span class="hljs-string">'lstrip()'</span></code>, <code class="python hljs"><span class="hljs-string">'rstrip()'</span></code> and <code class="python hljs"><span class="hljs-string">'rsplit()'</span></code>.</strong></li> |
316 |
| -<li><strong>Also: <code class="python hljs"><span class="hljs-string">'lower()'</span></code>, <code class="python hljs"><span class="hljs-string">'upper()'</span></code>, <code class="python hljs"><span class="hljs-string">'capitalize()'</span></code> and <code class="python hljs"><span class="hljs-string">'title()'</span></code>.</strong></li> |
| 317 | +<li><strong>Use <code class="python hljs"><span class="hljs-string">'unicodedata.normalize("NFC", <str>)'</span></code> on strings that may contain characters like <code class="python hljs"><span class="hljs-string">'Ö'</span></code> before comparing them, because they can be stored as one or two characters.</strong></li> |
317 | 318 | </ul>
|
318 |
| -<div><h3 id="propertymethods">Property Methods</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓ |
319 |
| -┃ │ [ !#$%…] │ [a-zA-Z] │ [¼½¾] │ [²³¹] │ [0-9] ┃ |
320 |
| -┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┨ |
321 |
| -┃ isprintable() │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃ |
322 |
| -┃ isalnum() │ │ ✓ │ ✓ │ ✓ │ ✓ ┃ |
323 |
| -┃ isnumeric() │ │ │ ✓ │ ✓ │ ✓ ┃ |
324 |
| -┃ isdigit() │ │ │ │ ✓ │ ✓ ┃ |
325 |
| -┃ isdecimal() │ │ │ │ │ ✓ ┃ |
326 |
| -┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛ |
| 319 | +<div><h3 id="propertymethods">Property Methods</h3><pre><code class="python language-python hljs"><bool> = <str>.isdecimal() <span class="hljs-comment"># Checks for [0-9].</span> |
| 320 | +<bool> = <str>.isdigit() <span class="hljs-comment"># Checks for [²³¹] and isdecimal().</span> |
| 321 | +<bool> = <str>.isnumeric() <span class="hljs-comment"># Checks for [¼½¾] and isdigit().</span> |
| 322 | +<bool> = <str>.isalnum() <span class="hljs-comment"># Checks for [a-zA-Z] and isnumeric().</span> |
| 323 | +<bool> = <str>.isprintable() <span class="hljs-comment"># Checks for [ !#$%…] and isalnum().</span> |
| 324 | +<bool> = <str>.isspace() <span class="hljs-comment"># Checks for [ \t\n\r\f\v\x1c-\x1f\x85\xa0…].</span> |
327 | 325 | </code></pre></div>
|
328 | 326 |
|
329 |
| -<ul> |
330 |
| -<li><strong><code class="python hljs"><span class="hljs-string">'isspace()'</span></code> checks for whitespaces: <code class="python hljs"><span class="hljs-string">'[ \t\n\r\f\v\x1c-\x1f\x85\xa0\u1680…]'</span></code>.</strong></li> |
331 |
| -</ul> |
332 |
| -<div><h2 id="regex"><a href="#regex" name="regex">#</a>Regex</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> re |
333 |
| -<str> = re.sub(<regex>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span> |
| 327 | +<div><h2 id="regex"><a href="#regex" name="regex">#</a>Regex</h2><p><strong>Functions for regular expression matching.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> re |
| 328 | +</code></pre></div> |
| 329 | + |
| 330 | + |
| 331 | +<pre><code class="python language-python hljs"><str> = re.sub(<regex>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span> |
334 | 332 | <list> = re.findall(<regex>, text) <span class="hljs-comment"># Returns all occurrences as strings.</span>
|
335 | 333 | <list> = re.split(<regex>, text, maxsplit=<span class="hljs-number">0</span>) <span class="hljs-comment"># Add brackets around regex to include matches.</span>
|
336 |
| -<Match> = re.search(<regex>, text) <span class="hljs-comment"># Searches for first occurrence of the pattern.</span> |
| 334 | +<Match> = re.search(<regex>, text) <span class="hljs-comment"># First occurrence of the pattern or None.</span> |
337 | 335 | <Match> = re.match(<regex>, text) <span class="hljs-comment"># Searches only at the beginning of the text.</span>
|
338 | 336 | <iter> = re.finditer(<regex>, text) <span class="hljs-comment"># Returns all occurrences as Match objects.</span>
|
339 |
| -</code></pre></div> |
340 |
| - |
| 337 | +</code></pre> |
341 | 338 | <ul>
|
342 | 339 | <li><strong>Argument 'new' can be a function that accepts a Match object and returns a string.</strong></li>
|
343 |
| -<li><strong>Search() and match() return None if they can't find a match.</strong></li> |
344 | 340 | <li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.IGNORECASE'</span></code> can be used with all functions.</strong></li>
|
345 | 341 | <li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.MULTILINE'</span></code> makes <code class="python hljs"><span class="hljs-string">'^'</span></code> and <code class="python hljs"><span class="hljs-string">'$'</span></code> match the start/end of each line.</strong></li>
|
346 | 342 | <li><strong>Argument <code class="python hljs"><span class="hljs-string">'flags=re.DOTALL'</span></code> makes <code class="python hljs"><span class="hljs-string">'.'</span></code> also accept the <code class="python hljs"><span class="hljs-string">'\n'</span></code>.</strong></li>
|
@@ -2929,7 +2925,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2929 | 2925 |
|
2930 | 2926 |
|
2931 | 2927 | <footer>
|
2932 |
| - <aside>October 4, 2023</aside> |
| 2928 | + <aside>October 11, 2023</aside> |
2933 | 2929 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2934 | 2930 | </footer>
|
2935 | 2931 |
|
|
0 commit comments