polars.Expr.str.strip_chars_start#

Expr.str.strip_chars_start(characters: IntoExprColumn | None = None) Expr[source]#

Remove leading characters.

Note

This method strips any characters present in characters from the start of the input, no matter their order. To strip a prefix (i.e. a “word” of characters in a certain order), use strip_prefix() instead.

Parameters:
characters

The set of characters to be removed. All combinations of this set of characters will be stripped from the start of the string. If set to None (default), all leading whitespace is removed instead.

Examples

>>> df = pl.DataFrame({"foo": [" hello ", "\tworld"]})
>>> df.with_columns(foo_strip_start=pl.col("foo").str.strip_chars_start())
shape: (2, 2)
┌─────────┬─────────────────┐
│ foo     ┆ foo_strip_start │
│ ---     ┆ ---             │
│ str     ┆ str             │
╞═════════╪═════════════════╡
│  hello  ┆ hello           │
│   world   ┆ world           │
└─────────┴─────────────────┘

Characters can be stripped by passing a string as argument. Note that whitespace will not be stripped automatically when doing so.

>>> df.with_columns(
...     foo_strip_start=pl.col("foo").str.strip_chars_start("wod\t"),
... )
shape: (2, 2)
┌─────────┬─────────────────┐
│ foo     ┆ foo_strip_start │
│ ---     ┆ ---             │
│ str     ┆ str             │
╞═════════╪═════════════════╡
│  hello  ┆  hello          │
│   world   ┆ rld             │
└─────────┴─────────────────┘

The order of the provided characters does not matter, they behave like a set.

>>> pl.DataFrame({"foo": ["aabcdef"]}).with_columns(
...     foo_strip_start=pl.col("foo").str.strip_chars_start("cba")
... )
shape: (1, 2)
┌─────────┬─────────────────┐
│ foo     ┆ foo_strip_start │
│ ---     ┆ ---             │
│ str     ┆ str             │
╞═════════╪═════════════════╡
│ aabcdef ┆ def             │
└─────────┴─────────────────┘