<bool> = <sub_str> in <str> # Checks if string contains the substring.
<bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
<int> = <str>.find(<sub_str>) # Returns start index of the first match or -1.
<int> = <str>.index(<sub_str>) # Same, but raises ValueError if missing.
<int> = <str>.index(<sub_str>) # Same, but raises ValueError if there's no match.
```
```python
@ -1136,7 +1136,7 @@ class MyHashable:
* **With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.**
* **Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.**
* **When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.**
* **For proper alphabetical order pass `'key=locale.strxfrm'` to sorted() after running `'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'`.**
* **To sort collection of strings in proper alphabetical order pass `'key=locale.strxfrm'` to sorted() after running `'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'`.**
```python
from functools import total_ordering
@ -1184,7 +1184,8 @@ class Counter:
### Callable
* **All functions and classes have a call() method, hence are callable.**
* **When this cheatsheet uses `'<function>'` as an argument, it actually means `'<callable>'`.**
* **To check if object is callable use `'callable(<obj>)'`, `'isinstance(<obj>, collections.abc.Callable)'`, or `'isinstance(<obj>, typing.Callable)'`.**
* **When this cheatsheet uses `'<function>'` as an argument, it means `'<callable>'`.**
```python
class Counter:
def __init__(self):
@ -1337,7 +1338,7 @@ from enum import Enum, auto
class <enum_name>(Enum):
<member_name> = auto() # Increment of the last numeric value or 1.
<member_name> = <value> # Values don't have to be hashable.
<member_name> = <value>, <value> # Values can be collections (like this tuple).
<member_name> = <el_1>, <el_2># Values can be collections (this is a tuple).
```
* **Methods receive the member they were called on as the 'self' argument.**
* **Accessing a member named after a reserved keyword causes SyntaxError.**
<pre><codeclass="python language-python hljs"><bool> = <sub_str><spanclass="hljs-keyword">in</span><str><spanclass="hljs-comment"># Checks if string contains the substring.</span>
<bool> = <str>.startswith(<sub_str>) <spanclass="hljs-comment"># Pass tuple of strings for multiple options.</span>
<int> = <str>.find(<sub_str>) <spanclass="hljs-comment"># Returns start index of the first match or -1.</span>
<int> = <str>.index(<sub_str>) <spanclass="hljs-comment"># Same, but raises ValueError if missing.</span>
<int> = <str>.index(<sub_str>) <spanclass="hljs-comment"># Same, but raises ValueError if there's no match.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><str> = <str>.lower() <spanclass="hljs-comment"># Changes the case. Also upper/capitalize/title().</span>
<str> = <str>.replace(old, new [, count]) <spanclass="hljs-comment"># Replaces 'old' with 'new' at most 'count' times.</span>
@ -956,7 +956,7 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<li><strong>With 'total_ordering' decorator, you only need to provide eq() and one of lt(), gt(), le() or ge() special methods and the rest will be automatically generated.</strong></li>
<li><strong>Functions sorted() and min() only require lt() method, while max() only requires gt(). However, it is best to define them all so that confusion doesn't arise in other contexts.</strong></li>
<li><strong>When two lists, strings or dataclasses are compared, their values get compared in order until a pair of unequal values is found. The comparison of this two values is then returned. The shorter sequence is considered smaller in case of all values being equal.</strong></li>
<li><strong>For proper alphabetical order pass <codeclass="python hljs"><spanclass="hljs-string">'key=locale.strxfrm'</span></code> to sorted() after running <codeclass="python hljs"><spanclass="hljs-string">'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'</span></code>.</strong></li>
<li><strong>To sort collection of strings in proper alphabetical order pass <codeclass="python hljs"><spanclass="hljs-string">'key=locale.strxfrm'</span></code> to sorted() after running <codeclass="python hljs"><spanclass="hljs-string">'locale.setlocale(locale.LC_COLLATE, "en_US.UTF-8")'</span></code>.</strong></li>
@ -1000,7 +1000,8 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<li><strong>File objects returned by the <ahref="#open">open()</a> function, etc.</strong></li>
</ul><div><h3id="callable">Callable</h3><ul>
<li><strong>All functions and classes have a call() method, hence are callable.</strong></li>
<li><strong>When this cheatsheet uses <codeclass="python hljs"><spanclass="hljs-string">'<function>'</span></code> as an argument, it actually means <codeclass="python hljs"><spanclass="hljs-string">'<callable>'</span></code>.</strong></li>
<li><strong>To check if object is callable use <codeclass="python hljs"><spanclass="hljs-string">'callable(<obj>)'</span></code>, <codeclass="python hljs"><spanclass="hljs-string">'isinstance(<obj>, collections.abc.Callable)'</span></code>, or <codeclass="python hljs"><spanclass="hljs-string">'isinstance(<obj>, typing.Callable)'</span></code>.</strong></li>
<li><strong>When this cheatsheet uses <codeclass="python hljs"><spanclass="hljs-string">'<function>'</span></code> as an argument, it means <codeclass="python hljs"><spanclass="hljs-string">'<callable>'</span></code>.</strong></li>