polars.internals.expr.string.ExprStringNameSpace.starts_with#

ExprStringNameSpace.starts_with(sub: str) Expr[source]#

Check if string values start with a substring.

Parameters:
sub

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_column(
...     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  │
└────────┘