strings
import stringsMost string operations are also available as methods directly on str values. The strings module provides the same operations as free functions.
Functions
| Signature | Description |
|---|---|
strings.starts_with(value: str, prefix: str) -> bool | True if value starts with prefix |
strings.ends_with(value: str, suffix: str) -> bool | True if value ends with suffix |
strings.find(value: str, needle: str) -> int | Index of first occurrence, or -1 |
strings.from_int(value: int) -> str | Convert integer to string |
strings.split(value: str, sep: str) -> list[str] | Split by separator |
strings.replace(value: str, old: str, new: str) -> str | Replace all occurrences |
Example
import strings
def main() -> None: value = "a::b::c" print(value.split("::")) print(strings.split("x-y-z", "-")) print("compiler".replace("pile", "pact")) print(strings.replace("aaaa", "aa", "b")) print(strings.starts_with("compiler", "com")) print(strings.ends_with("compiler", "ler")) print(strings.find("compiler", "pil")) print(strings.find("compiler", "xyz")) print(strings.from_int(42))Method equivalents
def main() -> None: s = "hello world" print(s.starts_with("hello")) print(s.ends_with("world")) print(s.find("world")) print(s.split(" ")) print(s.replace("world", "py4"))