polars.Expr.str.zfill#

Expr.str.zfill(length: int | IntoExprColumn) Expr[source]#

Pad the start of the string with zeros until it reaches the given length.

A sign prefix (-) is handled by inserting the padding after the sign character rather than before.

Parameters:
length

Pad the string until it reaches this length. Strings with length equal to or greater than this value are returned as-is.

See also

pad_start

Notes

This method is intended for padding numeric strings. If your data contains non-ASCII characters, use pad_start() instead.

Examples

>>> df = pl.DataFrame({"a": [-1, 123, 999999, None]})
>>> df.with_columns(zfill=pl.col("a").cast(pl.String).str.zfill(4))
shape: (4, 2)
┌────────┬────────┐
│ a      ┆ zfill  │
│ ---    ┆ ---    │
│ i64    ┆ str    │
╞════════╪════════╡
│ -1     ┆ -001   │
│ 123    ┆ 0123   │
│ 999999 ┆ 999999 │
│ null   ┆ null   │
└────────┴────────┘