polars.Expr.str.starts_with#

Expr.str.starts_with(prefix: str | Expr) Expr[source]#

Check if string values start with a substring.

Parameters:
prefix

Prefix substring.

See also

contains

Check if string contains a substring that matches a regex.

ends_with

Check if string values end with a substring.

Examples

>>> df = pl.DataFrame({"fruits": ["apple", "mango", None]})
>>> df.with_columns(
...     pl.col("fruits").str.starts_with("app").alias("has_prefix"),
... )
shape: (3, 2)
┌────────┬────────────┐
│ fruits ┆ has_prefix │
│ ---    ┆ ---        │
│ str    ┆ bool       │
╞════════╪════════════╡
│ apple  ┆ true       │
│ mango  ┆ false      │
│ null   ┆ null       │
└────────┴────────────┘

Using starts_with as a filter condition:

>>> df.filter(pl.col("fruits").str.starts_with("app"))
shape: (1, 1)
┌────────┐
│ fruits │
│ ---    │
│ str    │
╞════════╡
│ apple  │
└────────┘