| 1 |
.. _str-ops: |
| 2 |
|
| 3 |
Native string operations |
| 4 |
======================== |
| 5 |
|
| 6 |
These ``str`` operations have fast, optimized implementations. Other |
| 7 |
string operations use generic implementations that are often slower. |
| 8 |
|
| 9 |
Construction |
| 10 |
------------ |
| 11 |
|
| 12 |
* String literal |
| 13 |
* ``str(x: int)`` |
| 14 |
* ``str(x: object)`` |
| 15 |
* ``repr(x: int)`` |
| 16 |
* ``repr(x: object)`` |
| 17 |
|
| 18 |
.. note:: |
| 19 |
|
| 20 |
:ref:`librt.strings.StringWriter <librt-string-writer>` can be used to efficiently |
| 21 |
construct strings in compiled code. |
| 22 |
|
| 23 |
Operators |
| 24 |
--------- |
| 25 |
|
| 26 |
* Concatenation (``s1 + s2``) |
| 27 |
* Indexing (``s[n]``; also ``ord(s[n])``, which avoids the temporary length-1 string) |
| 28 |
* Slicing (``s[n:m]``, ``s[n:]``, ``s[:m]``) |
| 29 |
* Repetition (``s * n``, ``n * s``) |
| 30 |
* Comparisons (``==``, ``!=``) |
| 31 |
* Augmented assignment (``s1 += s2``) |
| 32 |
* Containment (``s1 in s2``) |
| 33 |
|
| 34 |
.. _str-methods: |
| 35 |
|
| 36 |
Methods |
| 37 |
------- |
| 38 |
|
| 39 |
* ``s.encode()`` |
| 40 |
* ``s.encode(encoding: str)`` |
| 41 |
* ``s.encode(encoding: str, errors: str)`` |
| 42 |
* ``s1.endswith(s2: str)`` |
| 43 |
* ``s1.endswith(t: tuple[str, ...])`` |
| 44 |
* ``s1.find(s2: str)`` |
| 45 |
* ``s1.find(s2: str, start: int)`` |
| 46 |
* ``s1.find(s2: str, start: int, end: int)`` |
| 47 |
* ``s.isalnum()`` |
| 48 |
* ``s.isdigit()`` |
| 49 |
* ``s.isspace()`` |
| 50 |
* ``s.lower()`` |
| 51 |
* ``s.join(x: Iterable)`` |
| 52 |
* ``s.lstrip()`` |
| 53 |
* ``s.lstrip(chars: str)`` |
| 54 |
* ``s.partition(sep: str)`` |
| 55 |
* ``s.removeprefix(prefix: str)`` |
| 56 |
* ``s.removesuffix(suffix: str)`` |
| 57 |
* ``s.replace(old: str, new: str)`` |
| 58 |
* ``s.replace(old: str, new: str, count: int)`` |
| 59 |
* ``s1.rfind(s2: str)`` |
| 60 |
* ``s1.rfind(s2: str, start: int)`` |
| 61 |
* ``s1.rfind(s2: str, start: int, end: int)`` |
| 62 |
* ``s.rpartition(sep: str)`` |
| 63 |
* ``s.rsplit()`` |
| 64 |
* ``s.rsplit(sep: str)`` |
| 65 |
* ``s.rsplit(sep: str, maxsplit: int)`` |
| 66 |
* ``s.rstrip()`` |
| 67 |
* ``s.rstrip(chars: str)`` |
| 68 |
* ``s.split()`` |
| 69 |
* ``s.split(sep: str)`` |
| 70 |
* ``s.split(sep: str, maxsplit: int)`` |
| 71 |
* ``s.splitlines()`` |
| 72 |
* ``s.splitlines(keepends: bool)`` |
| 73 |
* ``s1.startswith(s2: str)`` |
| 74 |
* ``s1.startswith(t: tuple[str, ...])`` |
| 75 |
* ``s.upper()`` |
| 76 |
* ``s.strip()`` |
| 77 |
* ``s.strip(chars: str)`` |
| 78 |
|
| 79 |
.. note:: |
| 80 |
|
| 81 |
:ref:`bytes.decode() <bytes-methods>` is also optimized. |
| 82 |
|
| 83 |
Formatting |
| 84 |
---------- |
| 85 |
|
| 86 |
A subset of these common string formatting expressions are optimized: |
| 87 |
|
| 88 |
* F-strings |
| 89 |
* ``"...".format(...)`` |
| 90 |
* ``"..." % (...)`` |
| 91 |
|
| 92 |
Functions |
| 93 |
--------- |
| 94 |
|
| 95 |
* ``len(s: str)`` |
| 96 |
* ``ord(s: str)`` |
| 97 |
|
| 98 |
* Calls with a literal argument are treated as compile-time integer constants (e.g. |
| 99 |
``ord("A")`` is equivalent to 65). |