polars.Expr.bin.starts_with#

Expr.bin.starts_with(prefix: bytes) Expr[source]#

Check if values start with a binary substring.

Parameters:
prefix

Prefix substring.

Returns:
Expr

Expression of data type Boolean.

See also

ends_with

Check if the binary substring exists at the end

contains

Check if the binary substring exists anywhere

Examples

>>> colors = pl.DataFrame(
...     {
...         "name": ["black", "yellow", "blue"],
...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
...     }
... )
>>> colors.select(
...     "name",
...     pl.col("code").bin.encode("hex").alias("code_encoded_hex"),
...     pl.col("code").bin.contains(b"\xff").alias("contains_ff"),
...     pl.col("code").bin.starts_with(b"\xff").alias("starts_with_ff"),
...     pl.col("code").bin.ends_with(b"\xff").alias("ends_with_ff"),
... )
shape: (3, 5)
┌────────┬──────────────────┬─────────────┬────────────────┬──────────────┐
│ name   ┆ code_encoded_hex ┆ contains_ff ┆ starts_with_ff ┆ ends_with_ff │
│ ---    ┆ ---              ┆ ---         ┆ ---            ┆ ---          │
│ str    ┆ str              ┆ bool        ┆ bool           ┆ bool         │
╞════════╪══════════════════╪═════════════╪════════════════╪══════════════╡
│ black  ┆ 000000           ┆ false       ┆ false          ┆ false        │
│ yellow ┆ ffff00           ┆ true        ┆ true           ┆ false        │
│ blue   ┆ 0000ff           ┆ true        ┆ false          ┆ true         │
└────────┴──────────────────┴─────────────┴────────────────┴──────────────┘