Sentinel
Language: Slices
Slices are an efficient way to create a substring or sublist from an existing string or list, respectively.
The syntax for creating a slice is:
a[low : high]
low
is the lowest index to slice from. The resulting slice includes
the value at low
. high
is the index to slice to. The resulting slice
will not include the value at high
. The length of the resulting list
or string is always high - low
.
a = [1, 2, 3, 4, 5]
b = a[1:4] // [2, 3, 4]
a = "hello"
b = a[1:4] // "ell"
Convenience Shorthands
Some convenience shorthands are available by omitting the value for either the
low or high index in the slice expression: omitting low
implies a low index of
0, and omitting high
implies a high index of the length of the list.
a = [1, 2, 3, 4, 5]
b = a[:2] // [1, 2] (same as a[0:2])
b = a[2:] // [3, 4, 5] (same as a[2:length(a)])