Skip to content

strings

import strings

Most string operations are also available as methods directly on str values. The strings module provides the same operations as free functions.

Functions

SignatureDescription
strings.starts_with(value: str, prefix: str) -> boolTrue if value starts with prefix
strings.ends_with(value: str, suffix: str) -> boolTrue if value ends with suffix
strings.find(value: str, needle: str) -> intIndex of first occurrence, or -1
strings.from_int(value: int) -> strConvert integer to string
strings.split(value: str, sep: str) -> list[str]Split by separator
strings.replace(value: str, old: str, new: str) -> strReplace 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"))