Browse Source

String

pull/46/head
Jure Šorn 5 years ago
parent
commit
0ec4301624
3 changed files with 40 additions and 27 deletions
  1. 30
      README.md
  2. 23
      index.html
  3. 14
      web/script_2.js

30
README.md

@ -320,35 +320,33 @@ String
<int> = <str>.index(<sub_str>) # Same but raises ValueError if missing.
```
```python
<bool> = <str>.isdecimal() # True if str contains only [0-9], [٠-٩], …
<bool> = <str>.isdigit() # Also true if str contains '¹²³…'.
<bool> = <str>.isnumeric() # Also true if str contains '¼½¾…'.
```
```python
<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.
<list> = textwrap.wrap(<str>, width) # Nicely breaks string into lines.
```
* **Also: `'lstrip()'`, `'rstrip()'`.**
* **Also: `'lower()'`, `'upper()'`, `'capitalize()'` and `'title()'`.**
### Properties
```text
+---------------+----------+----------+----------+----------+----------+----------+
| | [\t\n\r] | [ !#$%…] | [A-Za-z] | [¼½¾…] | [¹²³…] | [0-9] |
+---------------+----------+----------+----------+----------+----------+----------+
| isprintable() | | yes | yes | yes | yes | yes |
| isalnum() | | | yes | yes | yes | yes |
| isnumeric() | | | | yes | yes | yes |
| isdigit() | | | | | yes | yes |
| isdecimal() | | | | | | yes |
+---------------+----------+----------+----------+----------+----------+----------+
```
### Char
```python
<str> = chr(<int>) # Converts int to unicode char.
<int> = ord(<str>) # Converts unicode char to int.
```
```python
>>> ord('0'), ord('9')
(48, 57)
>>> ord('A'), ord('Z')
(65, 90)
>>> ord('a'), ord('z')
(97, 122)
```
Regex
-----

23
index.html

@ -432,28 +432,29 @@ to_exclusive = &lt;range&gt;.stop
&lt;int&gt; = &lt;str&gt;.find(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of first match or -1.</span>
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Same but raises ValueError if missing.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;str&gt;.isdecimal() <span class="hljs-comment"># True if str contains only [0-9], [٠-٩], …</span>
&lt;bool&gt; = &lt;str&gt;.isdigit() <span class="hljs-comment"># Also true if str contains '¹²³…'.</span>
&lt;bool&gt; = &lt;str&gt;.isnumeric() <span class="hljs-comment"># Also true if str contains '¼½¾…'.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.replace(old, new [, count]) <span class="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
&lt;str&gt; = &lt;str&gt;.translate(&lt;table&gt;) <span class="hljs-comment"># Use `str.maketrans(&lt;dict&gt;)` to generate table.</span>
&lt;list&gt; = textwrap.wrap(&lt;str&gt;, width) <span class="hljs-comment"># Nicely breaks string into lines.</span>
</code></pre>
<ul>
<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>
<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>
</ul>
<div><h3 id="properties">Properties</h3><pre><code class="text language-text">+---------------+----------+----------+----------+----------+----------+----------+
| | [\t\n\r] | [ !#$%…] | [A-Za-z] | [¼½¾…] | [¹²³…] | [0-9] |
+---------------+----------+----------+----------+----------+----------+----------+
| isprintable() | | yes | yes | yes | yes | yes |
| isalnum() | | | yes | yes | yes | yes |
| isnumeric() | | | | yes | yes | yes |
| isdigit() | | | | | yes | yes |
| isdecimal() | | | | | | yes |
+---------------+----------+----------+----------+----------+----------+----------+
</code></pre></div>
<div><h3 id="char">Char</h3><pre><code class="python language-python hljs">&lt;str&gt; = chr(&lt;int&gt;) <span class="hljs-comment"># Converts int to unicode char.</span>
&lt;int&gt; = ord(&lt;str&gt;) <span class="hljs-comment"># Converts unicode char to int.</span>
</code></pre></div>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>ord(<span class="hljs-string">'0'</span>), ord(<span class="hljs-string">'9'</span>)
(<span class="hljs-number">48</span>, <span class="hljs-number">57</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>ord(<span class="hljs-string">'A'</span>), ord(<span class="hljs-string">'Z'</span>)
(<span class="hljs-number">65</span>, <span class="hljs-number">90</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>ord(<span class="hljs-string">'a'</span>), ord(<span class="hljs-string">'z'</span>)
(<span class="hljs-number">97</span>, <span class="hljs-number">122</span>)
</code></pre>
<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
&lt;str&gt; = re.sub(&lt;regex&gt;, new, text, count=<span class="hljs-number">0</span>) <span class="hljs-comment"># Substitutes all occurrences.</span>
&lt;list&gt; = re.findall(&lt;regex&gt;, text) <span class="hljs-comment"># Returns all occurrences.</span>

14
web/script_2.js

@ -205,6 +205,19 @@ const DIAGRAM_11_B =
'┃ 4 │ -2147483648 │ 0 │ 2147483647 ┃\n' +
'┗━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━┛\n';
const DIAGRAM_12_A =
'+---------------+----------+----------+----------+----------+----------+----------+\n';
const DIAGRAM_12_B =
'┏━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┓\n' +
'┃ │ [\\t\\n\\r] │ [ !#$%…] │ [A-Za-z] │ [¼½¾…] │ [¹²³…] │ [0-9] ┃\n' +
'┠───────────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┨\n' +
'┃ isprintable() │ │ ✓ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ isalnum() │ │ │ ✓ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ isnumeric() │ │ │ │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ isdigit() │ │ │ │ │ ✓ │ ✓ ┃\n' +
'┃ isdecimal() │ │ │ │ │ │ ✓ ┃\n' +
'┗━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┛\n';
// isFontAvailable:
(function(d){function c(c){b.style.fontFamily=c;e.appendChild(b);f=b.clientWidth;e.removeChild(b);return f}var f,e=d.body,b=d.createElement("span");b.innerHTML=Array(100).join("wi");b.style.cssText=["position:absolute","width:auto","font-size:128px","left:-99999px"].join(" !important;");var g=c("monospace"),h=c("serif"),k=c("sans-serif");window.isFontAvailable=function(b){return g!==c(b+",monospace")||k!==c(b+",sans-serif")||h!==c(b+",serif")}})(document);
@ -221,6 +234,7 @@ if (isFontAvailable('Menlo')) {
$(`code:contains(${DIAGRAM_9_A})`).html(DIAGRAM_9_B);
$(`code:contains(${DIAGRAM_10_A})`).html(DIAGRAM_10_B);
$(`code:contains(${DIAGRAM_11_A})`).html(DIAGRAM_11_B);
$(`code:contains(${DIAGRAM_12_A})`).html(DIAGRAM_12_B);
}
var isMobile = false;

Loading…
Cancel
Save