Skip to content

Commit 2bf44cb

Browse files
Docs: Add missing str and bytes methods for complete API coverage
str: Add 33 missing methods (predicates, case conversion, padding, split variants, search methods). Remove redundant methods table. bytes: Add 34 missing methods. Remove erroneous copy() method that doesn't exist on bytes type. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 768ffaf commit 2bf44cb

2 files changed

Lines changed: 104 additions & 29 deletions

File tree

docs/builtins/bytes.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,56 @@ The `bytes` type is an immutable sequence of bytes, while `bytearray` is the mut
1111
| `len()` | O(1) | Direct lookup |
1212
| `access[i]` | O(1) | Direct indexing |
1313
| `in` (membership) | O(n) | Linear search |
14-
| `index(x)` | O(n) | Linear search |
15-
| `count(x)` | O(n) | Linear scan |
16-
| `copy()` | O(n) | Creates new bytes object |
17-
| `join()` | O(n) | Single pass, n = total output length |
18-
| `split()` | O(n) | Single pass |
14+
| **Search** |||
1915
| `find(sub)` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | n = bytes length, m = pattern length |
16+
| `rfind(sub)` | O(n*m) worst case | Search from right |
17+
| `index(sub)` | O(n) | Like find() but raises ValueError |
18+
| `rindex(sub)` | O(n*m) worst case | Like rfind() but raises ValueError |
19+
| `count(sub)` | O(n) | Count non-overlapping occurrences |
20+
| `startswith(prefix)` | O(m) | m = prefix length |
21+
| `endswith(suffix)` | O(m) | m = suffix length |
22+
| **Replace/Translate** |||
2023
| `replace(old, new)` | O(n) | Creates new bytes object |
21-
| `decode()` | O(n) | Convert to string |
22-
| `hex()` | O(n) | Convert to hex |
24+
| `translate(table)` | O(n) | Single pass with table lookup |
25+
| `maketrans(from, to)` | O(k) | k = mapping size; static method |
26+
| `expandtabs(tabsize)` | O(n) | Replace tabs with spaces |
27+
| `removeprefix(prefix)` | O(n) | Returns slice if prefix matches (Python 3.9+) |
28+
| `removesuffix(suffix)` | O(n) | Returns slice if suffix matches (Python 3.9+) |
29+
| **Split/Join** |||
30+
| `split(sep)` | O(n) | Single pass |
31+
| `rsplit(sep)` | O(n) | Split from right |
32+
| `splitlines()` | O(n) | Split on line boundaries |
33+
| `partition(sep)` | O(n) | Split into 3-tuple at first sep |
34+
| `rpartition(sep)` | O(n) | Split into 3-tuple at last sep |
35+
| `join(iterable)` | O(n) | n = total output length |
36+
| **Case Conversion** |||
37+
| `upper()` | O(n) | ASCII uppercase |
38+
| `lower()` | O(n) | ASCII lowercase |
39+
| `capitalize()` | O(n) | Uppercase first, lowercase rest |
40+
| `title()` | O(n) | Titlecase words |
41+
| `swapcase()` | O(n) | Swap upper/lower |
42+
| **Stripping** |||
43+
| `strip(chars)` | O(n) | Remove from both ends |
44+
| `lstrip(chars)` | O(n) | Remove from left |
45+
| `rstrip(chars)` | O(n) | Remove from right |
46+
| **Padding/Alignment** |||
47+
| `center(width)` | O(n) | Pad both sides |
48+
| `ljust(width)` | O(n) | Pad right side |
49+
| `rjust(width)` | O(n) | Pad left side |
50+
| `zfill(width)` | O(n) | Pad with zeros |
51+
| **Predicates** |||
52+
| `isalnum()` | O(n) | Check alphanumeric |
53+
| `isalpha()` | O(n) | Check alphabetic |
54+
| `isascii()` | O(n) | Check ASCII (Python 3.7+) |
55+
| `isdigit()` | O(n) | Check digit chars |
56+
| `islower()` | O(n) | Check lowercase |
57+
| `isspace()` | O(n) | Check whitespace |
58+
| `istitle()` | O(n) | Check titlecase |
59+
| `isupper()` | O(n) | Check uppercase |
60+
| **Encoding** |||
61+
| `decode(encoding)` | O(n) | Convert to string |
62+
| `hex()` | O(n) | Convert to hex string |
63+
| `fromhex(string)` | O(n) | Create bytes from hex; class method |
2364

2465
*Note: bytes is immutable, so operations that appear to modify return new objects (O(n) space).*
2566

docs/builtins/str.md

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,65 @@ The `str` type is an immutable sequence of Unicode characters. Python strings ha
99
| `len()` | O(1) | Direct lookup |
1010
| `access[i]` | O(1) | Direct indexing |
1111
| `in` (substring) | O(n + m) worst for long strings, O(n*m) worst for pathological cases | Uses Two-Way / fastsearch algorithm in CPython (linear worst-case) |
12-
| `count(sub)` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | n = string, m = substring; uses Two-Way/fastsearch (linear worst-case) |
13-
| `find(sub)` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | Uses Two-Way / fastsearch algorithm in CPython (linear worst-case) |
14-
| `replace(old, new)` | O(n) | Single pass |
15-
| `split(sep)` | O(n) | Single pass |
16-
| `join()` | O(n) | n = total chars |
17-
| `upper()/lower()` | O(n) | Must process each char |
18-
| `strip()` | O(n) | May check both ends |
19-
| `startswith()`/`endswith()` | O(m) | m = prefix/suffix length |
20-
| `format()` | O(n) | n = template length |
2112
| `s + s` (concatenation) | O(n+m) | Creates new string |
2213
| `s * n` (repetition) | O(n\*len(s)) | Creates new string |
2314
| `slice [::2]` | O(k) | k = slice length |
24-
| `encode()` | O(n) | Convert to bytes |
15+
| **Search** |||
16+
| `find(sub)` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | Uses Two-Way / fastsearch algorithm in CPython (linear worst-case) |
17+
| `rfind(sub)` | O(n*m) worst case | Does not use Two-Way algorithm; uses backward Boyer-Moore-Horspool |
18+
| `index(sub)` | O(n + m) | Like find() but raises ValueError if not found |
19+
| `rindex(sub)` | O(n*m) worst case | Like rfind() but raises ValueError if not found |
20+
| `count(sub)` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | n = string, m = substring; uses Two-Way/fastsearch (linear worst-case) |
21+
| `startswith(prefix)` | O(m) | m = prefix length |
22+
| `endswith(suffix)` | O(m) | m = suffix length |
23+
| **Replace/Translate** |||
24+
| `replace(old, new)` | O(n) | Single pass |
25+
| `translate(table)` | O(n) | Single pass with table lookup |
26+
| `maketrans()` | O(k) | k = number of mappings; static method |
27+
| `expandtabs(tabsize)` | O(n) | Replace tabs with spaces |
28+
| `removeprefix(prefix)` | O(n) | Returns slice if prefix matches (Python 3.9+) |
29+
| `removesuffix(suffix)` | O(n) | Returns slice if suffix matches (Python 3.9+) |
30+
| **Split/Join** |||
31+
| `split(sep)` | O(n) | Single pass |
32+
| `rsplit(sep)` | O(n) | Split from right |
33+
| `splitlines()` | O(n) | Split on line boundaries |
34+
| `partition(sep)` | O(n) | Split into 3-tuple at first sep |
35+
| `rpartition(sep)` | O(n) | Split into 3-tuple at last sep |
36+
| `join(iterable)` | O(n) | n = total output chars |
37+
| **Case Conversion** |||
38+
| `upper()` | O(n) | Must process each char |
39+
| `lower()` | O(n) | Must process each char |
40+
| `capitalize()` | O(n) | Uppercase first, lowercase rest |
41+
| `title()` | O(n) | Titlecase words |
42+
| `swapcase()` | O(n) | Swap upper/lower |
43+
| `casefold()` | O(n) | Aggressive lowercase for caseless matching |
44+
| **Stripping** |||
45+
| `strip(chars)` | O(n) | Remove from both ends |
46+
| `lstrip(chars)` | O(n) | Remove from left |
47+
| `rstrip(chars)` | O(n) | Remove from right |
48+
| **Padding/Alignment** |||
49+
| `center(width)` | O(n) | Pad both sides |
50+
| `ljust(width)` | O(n) | Pad right side |
51+
| `rjust(width)` | O(n) | Pad left side |
52+
| `zfill(width)` | O(n) | Pad with zeros |
53+
| **Predicates** |||
54+
| `isalnum()` | O(n) | Check alphanumeric |
55+
| `isalpha()` | O(n) | Check alphabetic |
56+
| `isascii()` | O(n) | Check ASCII (Python 3.7+) |
57+
| `isdecimal()` | O(n) | Check decimal chars |
58+
| `isdigit()` | O(n) | Check digit chars |
59+
| `isidentifier()` | O(n) | Check valid identifier |
60+
| `islower()` | O(n) | Check lowercase |
61+
| `isnumeric()` | O(n) | Check numeric chars |
62+
| `isprintable()` | O(n) | Check printable |
63+
| `isspace()` | O(n) | Check whitespace |
64+
| `istitle()` | O(n) | Check titlecase |
65+
| `isupper()` | O(n) | Check uppercase |
66+
| **Formatting** |||
67+
| `format(*args)` | O(n) | n = template length |
68+
| `format_map(mapping)` | O(n) | Like format() with mapping |
69+
| **Encoding** |||
70+
| `encode(encoding)` | O(n) | Convert to bytes |
2571

2672
## Space Complexity
2773

@@ -76,18 +122,6 @@ result = "".join(str(i) for i in range(10000))
76122

77123
## Advanced Features
78124

79-
### String Methods Complexity
80-
81-
| Method | Complexity | Notes |
82-
|--------|-----------|-------|
83-
| `str.find()` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | CPython uses Two-Way / fastsearch algorithm (linear worst-case) |
84-
| `str.rfind()` | O(n*m) worst case | Does not use Two-Way algorithm; uses backward Boyer-Moore-Horspool |
85-
| `str.count()` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | Non-overlapping searches (Two-Way/fastsearch) |
86-
| `str.replace()` | O(n) | Single pass with copy |
87-
| `str.split()` | O(n) | Single pass |
88-
| `str.startswith()` | O(m) | m = prefix length |
89-
| `str.translate()` | O(n) | Single pass |
90-
91125
### Substring Search
92126

93127
```python

0 commit comments

Comments
 (0)