Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Trim surrounding whitespace

strip(), lstrip(), and rstrip() remove Unicode whitespace from both ends, the left end, or the right end. Each returns Result[str, AllocError]:

def main() -> Result[(), AllocError | IoError]:
    text = "  é🙂  "
    print(str.repr(text.strip())?)?
    print(str.repr(text.lstrip())?)?
    print(str.repr(text.rstrip())?)?
    print(str.repr(" \t\n".strip())?)?
    Ok(())
Result[str, AllocError].Ok("é🙂")
Result[str, AllocError].Ok("é🙂  ")
Result[str, AllocError].Ok("  é🙂")
Result[str, AllocError].Ok("")

They borrow the source and allocate only the final independent string. Empty and unchanged results still need that allocation. Interior whitespace is kept; NUL and zero-width space are not trimmed. These methods use Unicode White_Space and take no arguments; custom character sets are not supported yet.