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
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 │ └────────┴────────────┘
>>> df = pl.DataFrame( ... {"fruits": ["apple", "mango", "banana"], "prefix": ["app", "na", "ba"]} ... ) >>> df.with_columns( ... pl.col("fruits").str.starts_with(pl.col("prefix")).alias("has_prefix"), ... ) shape: (3, 3) ┌────────┬────────┬────────────┐ │ fruits ┆ prefix ┆ has_prefix │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ bool │ ╞════════╪════════╪════════════╡ │ apple ┆ app ┆ true │ │ mango ┆ na ┆ false │ │ banana ┆ ba ┆ true │ └────────┴────────┴────────────┘
Using
starts_with
as a filter condition:>>> df.filter(pl.col("fruits").str.starts_with("app")) shape: (1, 2) ┌────────┬────────┐ │ fruits ┆ prefix │ │ --- ┆ --- │ │ str ┆ str │ ╞════════╪════════╡ │ apple ┆ app │ └────────┴────────┘