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
| Function | Returns | Description |
is_empty(s) | Bool | True if len(s) == 0 |
is_blank(s) | Bool | True if the string is empty or contains only whitespace |
starts_with(s, prefix) | Bool | True if s begins with prefix |
ends_with(s, suffix) | Bool | True if s ends with suffix |
contains(s, sub) | Bool | True 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
| Function | Returns | Description |
repeat(s, times) | Str | Repeat s the given number of times |
pad_left(s, width, ch) | Str | Pad s on the left with ch until it reaches width |
pad_right(s, width, ch) | Str | Pad s on the right with ch until it reaches width |
join(items, separator) | Str | Concatenate strings with separator between |
count_occurrences(s, sub) | Int | Count non-overlapping occurrences of sub in s |
reverse_string(s) | Str | Reverse 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"