Skip to content

Commit e1d1639

Browse files
committed
String
1 parent 28240a2 commit e1d1639

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ list_of_chars = list(<str>)
5555
```
5656

5757
```python
58-
index = <list>.index(<el>) # Returns first index of item.
58+
index = <list>.index(<el>) # Returns first index of item or raises ValueError.
5959
<list>.insert(index, <el>) # Inserts item at index and moves the rest to the right.
6060
<el> = <list>.pop([index]) # Removes and returns item at index or from the end.
6161
<list>.remove(<el>) # Removes first occurrence of item or raises ValueError.
@@ -269,23 +269,28 @@ String
269269
```
270270

271271
```python
272-
<list> = <str>.split() # Splits on any whitespace character.
272+
<list> = <str>.split() # Splits on one or more whitespace characters.
273273
<list> = <str>.split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times.
274+
<list> = <str>.splitlines(keepends=False) # Splits on line breaks. Keeps them if 'keepends'.
274275
<str> = <str>.join(<collection>) # Joins elements using string as separator.
275276
```
276277

277278
```python
278279
<str> = <str>.replace(old, new [, count]) # Replaces 'old' with 'new' at most 'count' times.
279280
<bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
280281
<bool> = <str>.endswith(<sub_str>) # Pass tuple of strings for multiple options.
281-
<int> = <str>.index(<sub_str>) # Returns start index of first match.
282+
<int> = <str>.find(<sub_str>) # Returns start index of first match or -1.
283+
<int> = <str>.index(<sub_str>) # Same but raises ValueError.
282284
```
283285

284286
```python
285287
<bool> = <str>.isnumeric() # True if str contains only numeric characters.
286288
<list> = textwrap.wrap(<str>, width) # Nicely breaks string into lines.
287289
```
288290

291+
* **Also: `'lstrip()'`, `'rstrip()'`.**
292+
* **Also: `'lower()'`, `'upper()'`, `'capitalize()'` and `'title()'`.**
293+
289294
### Char
290295
```python
291296
<str> = chr(<int>) # Converts int to unicode char.

index.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ <h2 id="list"><a href="#list" name="list">#</a>List</h2>
237237
product_of_elems = functools.reduce(<span class="hljs-keyword">lambda</span> out, x: out * x, &lt;collection&gt;)
238238
list_of_chars = list(&lt;str&gt;)
239239
</code></pre>
240-
<pre><code class="python language-python hljs">index = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns first index of item.</span>
240+
<pre><code class="python language-python hljs">index = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns first index of item or raises ValueError.</span>
241241
&lt;list&gt;.insert(index, &lt;el&gt;) <span class="hljs-comment"># Inserts item at index and moves the rest to the right.</span>
242242
&lt;el&gt; = &lt;list&gt;.pop([index]) <span class="hljs-comment"># Removes and returns item at index or from the end.</span>
243243
&lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes first occurrence of item or raises ValueError.</span>
@@ -377,18 +377,24 @@ <h2 id="string"><a href="#string" name="string">#</a>String</h2>
377377
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span>
378378
&lt;str&gt; = &lt;str&gt;.strip(<span class="hljs-string">'&lt;chars&gt;'</span>) <span class="hljs-comment"># Strips all passed characters from both ends.</span>
379379
</code></pre>
380-
<pre><code class="python language-python hljs">&lt;list&gt; = &lt;str&gt;.split() <span class="hljs-comment"># Splits on any whitespace character.</span>
380+
<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>
381381
&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>
382+
&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>
382383
&lt;str&gt; = &lt;str&gt;.join(&lt;collection&gt;) <span class="hljs-comment"># Joins elements using string as separator.</span>
383384
</code></pre>
384385
<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>
385386
&lt;bool&gt; = &lt;str&gt;.startswith(&lt;sub_str&gt;) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
386387
&lt;bool&gt; = &lt;str&gt;.endswith(&lt;sub_str&gt;) <span class="hljs-comment"># Pass tuple of strings for multiple options.</span>
387-
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of first match.</span>
388+
&lt;int&gt; = &lt;str&gt;.find(&lt;sub_str&gt;) <span class="hljs-comment"># Returns start index of first match or -1.</span>
389+
&lt;int&gt; = &lt;str&gt;.index(&lt;sub_str&gt;) <span class="hljs-comment"># Same but raises ValueError.</span>
388390
</code></pre>
389391
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;str&gt;.isnumeric() <span class="hljs-comment"># True if str contains only numeric characters.</span>
390392
&lt;list&gt; = textwrap.wrap(&lt;str&gt;, width) <span class="hljs-comment"># Nicely breaks string into lines.</span>
391393
</code></pre>
394+
<ul>
395+
<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>
396+
<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>
397+
</ul>
392398
<h3 id="char">Char</h3>
393399
<pre><code class="python language-python hljs">&lt;str&gt; = chr(&lt;int&gt;) <span class="hljs-comment"># Converts int to unicode char.</span>
394400
&lt;int&gt; = ord(&lt;str&gt;) <span class="hljs-comment"># Converts unicode char to int.</span>

0 commit comments

Comments
 (0)