Standard Library

Text

The text module provides string utility functions that complement the built-in Str methods.

from text import starts_with, pad_left, join

let ok = starts_with("hello", "hel")   # True
let id = pad_left("42", 5, "0")        # "00042"
let csv = join(["a", "b", "c"], ", ")   # "a, b, c"

String testing

FunctionReturnsDescription
is_empty(s)BoolTrue if len(s) == 0
is_blank(s)BoolTrue if the string is empty or contains only whitespace
starts_with(s, prefix)BoolTrue if s begins with prefix
ends_with(s, suffix)BoolTrue if s ends with suffix
contains(s, sub)BoolTrue if sub appears anywhere in s
from text import is_empty, is_blank, starts_with, ends_with, contains

print(is_empty(""))           # True
print(is_blank("  \n"))       # True
print(starts_with("vary", "va"))  # True
print(ends_with("file.txt", ".txt"))  # True
print(contains("hello world", "world"))  # True

String transformation

FunctionReturnsDescription
repeat(s, times)StrRepeat s the given number of times
pad_left(s, width, ch)StrPad s on the left with ch until it reaches width
pad_right(s, width, ch)StrPad s on the right with ch until it reaches width
join(items, separator)StrConcatenate strings with separator between
count_occurrences(s, sub)IntCount non-overlapping occurrences of sub in s
reverse_string(s)StrReverse the characters in a string
from text import repeat, pad_left, pad_right, join, count_occurrences, reverse_string

let stars = repeat("*", 5)              # "*****"
let left = pad_left("7", 3, "0")        # "007"
let right = pad_right("hi", 5, ".")     # "hi..."
let csv = join(["x", "y", "z"], ", ")   # "x, y, z"
let n = count_occurrences("abcabc", "abc")  # 2
let rev = reverse_string("hello")       # "olleh"
← File Watching
CSV →