Skip to content

Get the number of characters in strings

Source code

Description

Get length of the strings as UInt32 (as number of characters). Use $str$len_bytes() to get the number of bytes.

Usage

<Expr>$str$len_chars()

Details

If you know that you are working with ASCII text, lengths will be equivalent, and faster (returns length in terms of the number of bytes).

Value

Expr of u32

Examples

library("polars")

pl$DataFrame(
  s = c("Café", NA, "345", "æøå")
)$select(
  pl$col("s"),
  pl$col("s")$str$len_bytes()$alias("lengths"),
  pl$col("s")$str$len_chars()$alias("n_chars")
)
#> shape: (4, 3)
#> ┌──────┬─────────┬─────────┐
#> │ s    ┆ lengths ┆ n_chars │
#> │ ---  ┆ ---     ┆ ---     │
#> │ str  ┆ u32     ┆ u32     │
#> ╞══════╪═════════╪═════════╡
#> │ Café ┆ 5       ┆ 4       │
#> │ null ┆ null    ┆ null    │
#> │ 345  ┆ 3       ┆ 3       │
#> │ æøå  ┆ 6       ┆ 3       │
#> └──────┴─────────┴─────────┘