polars.Series.str.slice#

Series.str.slice(
offset: int | IntoExprColumn,
length: int | IntoExprColumn | None = None,
) Series[source]#

Extract a substring from each string value.

Parameters:
offset

Start index. Negative indexing is supported.

length

Length of the slice. If set to None (default), the slice is taken to the end of the string.

Returns:
Series

Series of data type String.

Notes

Both the offset and length inputs are defined in terms of the number of characters in the (UTF8) string. A character is defined as a Unicode scalar value. A single character is represented by a single byte when working with ASCII text, and a maximum of 4 bytes otherwise.

Examples

>>> s = pl.Series(["pear", None, "papaya", "dragonfruit"])
>>> s.str.slice(-3)
shape: (4,)
Series: '' [str]
[
    "ear"
    null
    "aya"
    "uit"
]

Using the optional length parameter

>>> s.str.slice(4, length=3)
shape: (4,)
Series: '' [str]
[
    ""
    null
    "ya"
    "onf"
]