Reference · Strings

slice

signature
slice(list | str, int, int=) -> list | str

What it does

The part of a list or a string from a start position up to, but not including, an end position; without an end, up to the end. Positions count from 0; a negative position counts from the end. A string is sliced by characters, and the result is of the same kind as the input: a list gives a list, a string a string.

Examples

print(slice("hello", 1, 3), slice("hello", 2), slice("hello", -3))print(slice([1, 2, 3, 4], 1, 3), slice([1, 2, 3, 4], -2))
output
el llo llo[2, 3] [3, 4]

Edge cases

print(slice("hello", 0, 100), slice("hello", 10), slice("", 0, 2), slice("abc", -10, 2))print(slice("hello", 3, 1), slice([1, 2, 3, 4], 0, 0))print(slice("hello", -3, -1), slice([1, 2, 3], 1, -1))print(slice("héllo😀", 1, 2), slice("héllo😀", -1))
output
hello   ab []ll [2]é 😀

Errors

A first argument that is not a list or a string (null included), or a position that is not an int, is a runtime error, not an error value. The start is required.

this one fails
print(slice(null, 1))
output
line 1: `slice` expects a list or str, got null
this one fails
print(slice("abc", "1"))
output
line 1: `slice` expects an int as argument 2, got str

Chaining

In a pipeline the list or text comes first and the positions follow:

print("hello" | slice(1, 3), [1, 2, 3] | slice(1))let i = index_of("key=value", "=")print(slice("key=value", 0, i), slice("key=value", i + 1))print(range(10) | slice(-3) | map((n) => n * 2))
output
el [2, 3]key value[14, 16, 18]

See also

take, drop, index_of, len, split, first, last

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On slice(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo