|
56 | 56 |
|
57 | 57 | <body> |
58 | 58 | <header> |
59 | | - <aside>September 8, 2025</aside> |
| 59 | + <aside>September 16, 2025</aside> |
60 | 60 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
61 | 61 | </header> |
62 | 62 |
|
|
304 | 304 | ┃ decimal.Decimal │ ✓ │ │ │ │ ┃ |
305 | 305 | ┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛ |
306 | 306 | </code></pre> |
307 | | -<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> |
308 | | -<str> = <str>.strip(<span class="hljs-string">'<chars>'</span>) <span class="hljs-comment"># Strips passed characters. Also lstrip/rstrip().</span> |
| 307 | +<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> = <span class="hljs-string">'abc'</span> <span class="hljs-comment"># Also "abc". Interprets \n, \t, \x00-\xff, etc.</span> |
309 | 308 | </code></pre></div> |
310 | 309 |
|
311 | 310 |
|
312 | | -<pre><code class="python language-python hljs"><list> = <str>.split() <span class="hljs-comment"># Splits on one or more whitespace characters.</span> |
| 311 | +<pre><code class="python language-python hljs"><str> = <str>.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span> |
| 312 | +<str> = <str>.strip(<span class="hljs-string">'<chars>'</span>) <span class="hljs-comment"># Strips passed characters. Also lstrip/rstrip().</span> |
| 313 | +</code></pre> |
| 314 | +<pre><code class="python language-python hljs"><list> = <str>.split() <span class="hljs-comment"># Splits it on one or more whitespace characters.</span> |
313 | 315 | <list> = <str>.split(sep=<span class="hljs-keyword">None</span>, maxsplit=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Splits on 'sep' string at most 'maxsplit' times.</span> |
314 | 316 | <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> |
315 | | -<str> = <str>.join(<coll_of_strings>) <span class="hljs-comment"># Joins elements by using string as a separator.</span> |
| 317 | +<str> = <str>.join(<coll_of_strings>) <span class="hljs-comment"># Joins items by using the string as a separator.</span> |
316 | 318 | </code></pre> |
317 | | -<pre><code class="python language-python hljs"><bool> = <sub_str> <span class="hljs-keyword">in</span> <str> <span class="hljs-comment"># Checks if string contains the substring.</span> |
318 | | -<bool> = <str>.startswith(<sub_str>) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span> |
| 319 | +<pre><code class="python language-python hljs"><bool> = <sub_str> <span class="hljs-keyword">in</span> <str> <span class="hljs-comment"># Returns True if string contains the substring.</span> |
| 320 | +<bool> = <str>.startswith(<sub_str>) <span class="hljs-comment"># Pass tuple of strings to give multiple options.</span> |
319 | 321 | <int> = <str>.find(<sub_str>) <span class="hljs-comment"># Returns start index of the first match or -1.</span> |
320 | 322 | </code></pre> |
321 | 323 | <pre><code class="python language-python hljs"><str> = <str>.lower() <span class="hljs-comment"># Lowers the case. Also upper/capitalize/title().</span> |
322 | | -<str> = <str>.casefold() <span class="hljs-comment"># Same, but converts ẞ/ß to ss, Σ/ς to σ, etc.</span> |
| 324 | +<str> = <str>.casefold() <span class="hljs-comment"># Lower() that converts ẞ/ß to ss, Σ/ς to σ, etc.</span> |
323 | 325 | <str> = <str>.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span> |
324 | 326 | <str> = <str>.translate(<table>) <span class="hljs-comment"># Use `str.maketrans(<dict>)` to generate table.</span> |
325 | 327 | </code></pre> |
326 | | -<pre><code class="python language-python hljs"><str> = chr(<int>) <span class="hljs-comment"># Converts passed integer to Unicode character.</span> |
327 | | -<int> = ord(<str>) <span class="hljs-comment"># Converts passed Unicode character to integer.</span> |
| 328 | +<pre><code class="python language-python hljs"><str> = chr(<int>) <span class="hljs-comment"># Converts passed integer into Unicode character.</span> |
| 329 | +<int> = ord(<str>) <span class="hljs-comment"># Converts passed Unicode character into integer.</span> |
328 | 330 | </code></pre> |
329 | 331 | <ul> |
330 | 332 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'unicodedata.normalize("NFC", <str>)'</span></code> on strings like <code class="python hljs"><span class="hljs-string">'Motörhead'</span></code> before comparing them to other strings, because <code class="python hljs"><span class="hljs-string">'ö'</span></code> can be stored as one or two characters.</strong></li> |
331 | 333 | <li><strong><code class="python hljs"><span class="hljs-string">'NFC'</span></code> converts such characters to a single character, while <code class="python hljs"><span class="hljs-string">'NFD'</span></code> converts them to two.</strong></li> |
332 | 334 | </ul> |
333 | | -<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]. Also [०-९] and [٠-٩].</span> |
334 | | -<bool> = <str>.isdigit() <span class="hljs-comment"># Checks for [²³¹…] and isdecimal().</span> |
335 | | -<bool> = <str>.isnumeric() <span class="hljs-comment"># Checks for [¼½¾…], [零〇一…] and isdigit().</span> |
336 | | -<bool> = <str>.isalnum() <span class="hljs-comment"># Checks for [a-zA-Z…] and isnumeric().</span> |
337 | | -<bool> = <str>.isprintable() <span class="hljs-comment"># Checks for [ !#$%…] and isalnum().</span> |
338 | | -<bool> = <str>.isspace() <span class="hljs-comment"># Checks for [ \t\n\r\f\v\x1c-\x1f\x85…].</span> |
339 | | -</code></pre></div> |
340 | | - |
| 335 | +<pre><code class="python language-python hljs"><bool> = <str>.isdecimal() <span class="hljs-comment"># Checks all chars for [0-9]. Also [०-९], [٠-٩].</span> |
| 336 | +<bool> = <str>.isdigit() <span class="hljs-comment"># Checks for [²³¹…] and isdecimal(). Also [፩-፱].</span> |
| 337 | +<bool> = <str>.isnumeric() <span class="hljs-comment"># Checks for [¼½¾…] and isdigit(). Also [零〇一…].</span> |
| 338 | +<bool> = <str>.isalnum() <span class="hljs-comment"># Checks for [ABC…] and isnumeric(). Also [ªµº…].</span> |
| 339 | +<bool> = <str>.isprintable() <span class="hljs-comment"># Checks for [ !"#$…] and isalnum(). Also emojis.</span> |
| 340 | +<bool> = <str>.isspace() <span class="hljs-comment"># Checks for [ \t\n\r\f\v\x1c\x1d\x1e\x1f\x85…].</span> |
| 341 | +</code></pre> |
341 | 342 | <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 |
342 | 343 | <str> = re.sub(<span class="hljs-string">r'<regex>'</span>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences with 'new'.</span> |
343 | 344 | <list> = re.findall(<span class="hljs-string">r'<regex>'</span>, text) <span class="hljs-comment"># Returns all occurrences of the pattern.</span> |
@@ -2933,7 +2934,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
2933 | 2934 |
|
2934 | 2935 |
|
2935 | 2936 | <footer> |
2936 | | - <aside>September 8, 2025</aside> |
| 2937 | + <aside>September 16, 2025</aside> |
2937 | 2938 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
2938 | 2939 | </footer> |
2939 | 2940 |
|
|
0 commit comments