polars.Expr.str.strip#

Expr.str.strip(matches: str | None = None) Expr[source]#

Remove leading and trailing characters.

Parameters:
matches

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

Examples

>>> df = pl.DataFrame({"foo": [" hello ", "\tworld"]})
>>> df.select(pl.col("foo").str.strip())
shape: (2, 1)
┌───────┐
│ foo   │
│ ---   │
│ str   │
╞═══════╡
│ hello │
│ world │
└───────┘

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

>>> df.select(pl.col("foo").str.strip("od\t"))
shape: (2, 1)
┌─────────┐
│ foo     │
│ ---     │
│ str     │
╞═════════╡
│  hello  │
│ worl    │
└─────────┘