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

Splitting text that is already in memory

splitlines() returns an independent list without line endings. Pass True to keep the original endings. A final newline does not add an extra empty line, and empty text produces an empty list. Unlike file reads, this method also recognizes Unicode line/paragraph separators and the other Python-style text line boundaries.

def main() -> Result[(), AllocError | IoError]:
    print("first\r\n\nlast\n".splitlines()?)?
    print("first\r\nlast".splitlines(True)?)?
    print("".splitlines()?)?
    Ok(())
["first", "", "last"]
["first\r\n", "last"]
[]