shape: (3, 1)
| Provinces |
|---|
| str |
| "Newfoundland" |
| "Saskatchewan" |
| "Manitoba" |
str.head
Joram Mutenge
2025-11-24
Sometimes you may need to extract the first five or seven characters from a text value. This is useful when working with long strings. The following example uses a dataframe of Canadian provinces.
| Provinces |
|---|
| str |
| "Newfoundland" |
| "Saskatchewan" |
| "Manitoba" |
To get the first five letters of each province name, use the Polars expression str.head like this:
| Provinces | First_5_Chars |
|---|---|
| str | str |
| "Newfoundland" | "Newfo" |
| "Saskatchewan" | "Saska" |
| "Manitoba" | "Manit" |
To get the last five letters, use the Polars expression str.tail.
Enroll in my Polars course to learn more.