Skip to content

daft.functions.substr#

substr #

substr(expr: Expression, start: int | Expression, length: int | Expression | None = None) -> Expression

Extract a substring from a string, starting at a specified index and extending for a given length.

Returns:

Name Type Description
Expression Expression

A String expression representing the extracted substring.

Note

If length is not provided, the substring will include all characters from start to the end of the string.

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import substr
>>> df = daft.from_pydict({"x": ["daft", "query", "engine"]})
>>> df = df.select(substr(df["x"], 2, 4))
>>> df.show()
╭────────╮
│ x      │
│ ---    │
│ String │
╞════════╡
│ ft     │
├╌╌╌╌╌╌╌╌┤
│ ery    │
├╌╌╌╌╌╌╌╌┤
│ gine   │
╰────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
def substr(expr: Expression, start: int | Expression, length: int | Expression | None = None) -> Expression:
    """Extract a substring from a string, starting at a specified index and extending for a given length.

    Returns:
        Expression: A String expression representing the extracted substring.

    Note:
        If `length` is not provided, the substring will include all characters from `start` to the end of the string.

    Examples:
        >>> import daft
        >>> from daft.functions import substr
        >>> df = daft.from_pydict({"x": ["daft", "query", "engine"]})
        >>> df = df.select(substr(df["x"], 2, 4))
        >>> df.show()
        ╭────────╮
        │ x      │
        │ ---    │
        │ String │
        ╞════════╡
        │ ft     │
        ├╌╌╌╌╌╌╌╌┤
        │ ery    │
        ├╌╌╌╌╌╌╌╌┤
        │ gine   │
        ╰────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("substr", expr, start, length)