Skip to content

Commit 0e9cbd3

Browse files
committed
Bytes
1 parent 96f2f7c commit 0e9cbd3

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ String
303303
<list> = <str>.split() # Splits on one or more whitespace characters.
304304
<list> = <str>.split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times.
305305
<list> = <str>.splitlines(keepends=False) # Splits on line breaks. Keeps them if 'keepends'.
306-
<str> = <str>.join(<collection>) # Joins elements using string as separator.
306+
<str> = <str>.join(<coll_of_strings>) # Joins elements using string as separator.
307307
```
308308

309309
```python
@@ -1475,23 +1475,24 @@ Bytes
14751475
**Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.**
14761476

14771477
```python
1478-
<bytes> = b'<str>'
1479-
<int> = <bytes>[<index>]
1480-
<bytes> = <bytes>[<slice>]
1481-
<ints> = list(<bytes>)
1482-
<bytes> = b''.join(<coll_of_bytes>)
1478+
<bytes> = b'<str>' # Only accepts ASCII characters and \x00 - \xff.
1479+
<int> = <bytes>[<index>] # Returns int in range from 0 to 255.
1480+
<bytes> = <bytes>[<slice>] # Returns bytes even if it has only one element.
1481+
<bytes> = <bytes>.join(<coll_of_bytes>) # Joins elements using bytes object as separator.
14831482
```
14841483

14851484
### Encode
14861485
```python
1487-
<bytes> = <str>.encode(encoding='utf-8')
1486+
<bytes> = <str>.encode('utf-8') # Or: bytes(<str>, 'utf-8')
1487+
<bytes> = bytes(<coll_of_ints>) # Ints must be in range from 0 to 255.
14881488
<bytes> = <int>.to_bytes(<length>, byteorder='big|little', signed=False)
14891489
<bytes> = bytes.fromhex('<hex>')
14901490
```
14911491

14921492
### Decode
14931493
```python
1494-
<str> = <bytes>.decode(encoding='utf-8')
1494+
<str> = <bytes>.decode('utf-8') # Or: str(<bytes>, 'utf-8')
1495+
<list> = list(<bytes>) # Returns ints in range from 0 to 255.
14951496
<int> = int.from_bytes(<bytes>, byteorder='big|little', signed=False)
14961497
'<hex>' = <bytes>.hex()
14971498
```

index.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ <h2 id="string"><a href="#string" name="string">#</a>String</h2>
405405
<pre><code class="python language-python hljs">&lt;list&gt; = &lt;str&gt;.split() <span class="hljs-comment"># Splits on one or more whitespace characters.</span>
406406
&lt;list&gt; = &lt;str&gt;.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>
407407
&lt;list&gt; = &lt;str&gt;.splitlines(keepends=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># Splits on line breaks. Keeps them if 'keepends'.</span>
408-
&lt;str&gt; = &lt;str&gt;.join(&lt;collection&gt;) <span class="hljs-comment"># Joins elements using string as separator.</span>
408+
&lt;str&gt; = &lt;str&gt;.join(&lt;coll_of_strings&gt;) <span class="hljs-comment"># Joins elements using string as separator.</span>
409409
</code></pre>
410410
<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>
411411
&lt;bool&gt; = &lt;str&gt;.startswith(&lt;sub_str&gt;) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
@@ -1289,19 +1289,20 @@ <h3 id="write">Write</h3>
12891289
</code></pre>
12901290
<h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2>
12911291
<p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.</strong></p>
1292-
<pre><code class="python language-python hljs">&lt;bytes&gt; = <span class="hljs-string">b'&lt;str&gt;'</span>
1293-
&lt;int&gt; = &lt;bytes&gt;[&lt;index&gt;]
1294-
&lt;bytes&gt; = &lt;bytes&gt;[&lt;slice&gt;]
1295-
&lt;ints&gt; = list(&lt;bytes&gt;)
1296-
&lt;bytes&gt; = <span class="hljs-string">b''</span>.join(&lt;coll_of_bytes&gt;)
1292+
<pre><code class="python language-python hljs">&lt;bytes&gt; = <span class="hljs-string">b'&lt;str&gt;'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00 - \xff.</span>
1293+
&lt;int&gt; = &lt;bytes&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns int in range from 0 to 255.</span>
1294+
&lt;bytes&gt; = &lt;bytes&gt;[&lt;slice&gt;] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
1295+
&lt;bytes&gt; = &lt;bytes&gt;.join(&lt;coll_of_bytes&gt;) <span class="hljs-comment"># Joins elements using bytes object as separator. </span>
12971296
</code></pre>
12981297
<h3 id="encode-1">Encode</h3>
1299-
<pre><code class="python language-python hljs">&lt;bytes&gt; = &lt;str&gt;.encode(encoding=<span class="hljs-string">'utf-8'</span>)
1298+
<pre><code class="python language-python hljs">&lt;bytes&gt; = &lt;str&gt;.encode(<span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: bytes(&lt;str&gt;, 'utf-8')</span>
1299+
&lt;bytes&gt; = bytes(&lt;coll_of_ints&gt;) <span class="hljs-comment"># Ints must be in range from 0 to 255.</span>
13001300
&lt;bytes&gt; = &lt;int&gt;.to_bytes(&lt;length&gt;, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>)
13011301
&lt;bytes&gt; = bytes.fromhex(<span class="hljs-string">'&lt;hex&gt;'</span>)
13021302
</code></pre>
13031303
<h3 id="decode-1">Decode</h3>
1304-
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;bytes&gt;.decode(encoding=<span class="hljs-string">'utf-8'</span>)
1304+
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;bytes&gt;.decode(<span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: str(&lt;bytes&gt;, 'utf-8')</span>
1305+
&lt;list&gt; = list(&lt;bytes&gt;) <span class="hljs-comment"># Returns ints in range from 0 to 255.</span>
13051306
&lt;int&gt; = int.from_bytes(&lt;bytes&gt;, byteorder=<span class="hljs-string">'big|little'</span>, signed=<span class="hljs-keyword">False</span>)
13061307
<span class="hljs-string">'&lt;hex&gt;'</span> = &lt;bytes&gt;.hex()
13071308
</code></pre>

0 commit comments

Comments
 (0)