polars.Expr.str.strip_chars#
- Expr.str.strip_chars(characters: IntoExprColumn | None = None) Expr [source]#
Remove leading and trailing characters.
- Parameters:
- characters
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", "\nworld"]}) >>> df shape: (2, 1) ┌────────┐ │ foo │ │ --- │ │ str │ ╞════════╡ │ hello │ │ │ │ world │ └────────┘
>>> df.with_columns(foo_stripped=pl.col("foo").str.strip_chars()) shape: (2, 2) ┌────────┬──────────────┐ │ foo ┆ foo_stripped │ │ --- ┆ --- │ │ 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, unless that whitespace is also included in the string.
>>> df.with_columns(foo_stripped=pl.col("foo").str.strip_chars("ow\n")) shape: (2, 2) ┌────────┬──────────────┐ │ foo ┆ foo_stripped │ │ --- ┆ --- │ │ str ┆ str │ ╞════════╪══════════════╡ │ hello ┆ hell │ │ ┆ rld │ │ world ┆ │ └────────┴──────────────┘