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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
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)