You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/builtins/str.md
+56-22Lines changed: 56 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,19 +9,65 @@ The `str` type is an immutable sequence of Unicode characters. Python strings ha
9
9
|`len()`| O(1) | Direct lookup |
10
10
|`access[i]`| O(1) | Direct indexing |
11
11
|`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 |
|`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 |
0 commit comments