CKTIS / Python
slice()
Guarda inicio, fin excluido y paso para un corte posterior.
Versión: Python 3.14; repository image, production runtime not attested
Sintaxis
slice(stop, /)
slice(start, stop, step=None, /)Guarda inicio, fin excluido y paso para un corte posterior.
Un solo argumento indica solo stop. Con paso −1 y ambos límites None, indices(4) da (3, -1, -1) para range, es decir 3, 2, 1, 0 y el texto dcba.
El paso cero falla al aplicar el corte.
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"
Ejemplos
selection = slice(None, None, -1)
print("abcd"[selection])
print(selection.indices(4))
Resultado
dcba (3, -1, -1)
El paso cero falla al aplicar el corte.