|
432 | 432 | <int> = <str>.find(<sub_str>) <span class="hljs-comment"># Returns start index of first match or -1.</span>
|
433 | 433 | <int> = <str>.index(<sub_str>) <span class="hljs-comment"># Same but raises ValueError if missing.</span>
|
434 | 434 | </code></pre>
|
435 |
| -<pre><code class="python language-python hljs"><bool> = <str>.isdecimal() <span class="hljs-comment"># True if str contains only [0-9], [٠-٩], …</span> |
436 |
| -<bool> = <str>.isdigit() <span class="hljs-comment"># Also true if str contains '¹²³…'.</span> |
437 |
| -<bool> = <str>.isnumeric() <span class="hljs-comment"># Also true if str contains '¼½¾…'.</span> |
438 |
| -</code></pre> |
439 | 435 | <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>
|
| 436 | +<str> = <str>.translate(<table>) <span class="hljs-comment"># Use `str.maketrans(<dict>)` to generate table.</span> |
440 | 437 | <list> = textwrap.wrap(<str>, width) <span class="hljs-comment"># Nicely breaks string into lines.</span>
|
441 | 438 | </code></pre>
|
442 | 439 | <ul>
|
443 | 440 | <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>.</strong></li>
|
444 | 441 | <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>
|
445 | 442 | </ul>
|
| 443 | +<div><h3 id="properties">Properties</h3><pre><code class="text language-text">+---------------+----------+----------+----------+----------+----------+----------+ |
| 444 | +| | [\t\n\r] | [ !#$%…] | [A-Za-z] | [¼½¾…] | [¹²³…] | [0-9] | |
| 445 | ++---------------+----------+----------+----------+----------+----------+----------+ |
| 446 | +| isprintable() | | yes | yes | yes | yes | yes | |
| 447 | +| isalnum() | | | yes | yes | yes | yes | |
| 448 | +| isnumeric() | | | | yes | yes | yes | |
| 449 | +| isdigit() | | | | | yes | yes | |
| 450 | +| isdecimal() | | | | | | yes | |
| 451 | ++---------------+----------+----------+----------+----------+----------+----------+ |
| 452 | +</code></pre></div> |
| 453 | + |
446 | 454 | <div><h3 id="char">Char</h3><pre><code class="python language-python hljs"><str> = chr(<int>) <span class="hljs-comment"># Converts int to unicode char.</span>
|
447 | 455 | <int> = ord(<str>) <span class="hljs-comment"># Converts unicode char to int.</span>
|
448 | 456 | </code></pre></div>
|
449 | 457 |
|
450 |
| -<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>ord(<span class="hljs-string">'0'</span>), ord(<span class="hljs-string">'9'</span>) |
451 |
| -(<span class="hljs-number">48</span>, <span class="hljs-number">57</span>) |
452 |
| -<span class="hljs-meta">>>> </span>ord(<span class="hljs-string">'A'</span>), ord(<span class="hljs-string">'Z'</span>) |
453 |
| -(<span class="hljs-number">65</span>, <span class="hljs-number">90</span>) |
454 |
| -<span class="hljs-meta">>>> </span>ord(<span class="hljs-string">'a'</span>), ord(<span class="hljs-string">'z'</span>) |
455 |
| -(<span class="hljs-number">97</span>, <span class="hljs-number">122</span>) |
456 |
| -</code></pre> |
457 | 458 | <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
|
458 | 459 | <str> = re.sub(<regex>, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences.</span>
|
459 | 460 | <list> = re.findall(<regex>, text) <span class="hljs-comment"># Returns all occurrences.</span>
|
|
0 commit comments