CKTIS / Python
slice()
Represents the bounds and step of a subsequence selection as an object.
Version: Python 3.14; repository image, production runtime not attested
Syntax
slice(stop, /)
slice(start, stop, step=None, /)Represents the bounds and step of a subsequence selection as an object.
Omitted bounds depend on direction. Step -1 with both bounds omitted visits every element from last to first.
Stores start, exclusive stop and step for later indexing. slice(stop) omits the start. For step -1 with both bounds None, indices(4) yields (3, -1, -1) for use in range, traversing indices 3, 2, 1 and 0 and producing dcba. A zero step is rejected when the slice is applied.
s = slice(None, None, -1)
assert s.indices(4) == (3, -1, -1) and list(range(*s.indices(4))) == [3, 2, 1, 0]
assert "abcd"[slice(2)] == "ab" and "abcd"[:: -2] == "db"
Examples
selection = slice(None, None, -1)
print("abcd"[selection])
print(selection.indices(4))
Result
dcba (3, -1, -1)
Stores start, exclusive stop and step for later indexing. slice(stop) omits the start. For step -1 with both bounds None, indices(4) yields (3, -1, -1) for use in range, traversing indices 3, 2, 1 and 0 and producing dcba. A zero step is rejected when the slice is applied.