CKTIS / Python
slice()
Guarda início, fim excluído e passo para um corte posterior.
Versão: Python 3.14; repository image, production runtime not attested
Sintaxe
slice(stop, /)
slice(start, stop, step=None, /)Guarda início, fim excluído e passo para um corte posterior.
Um só argumento indica apenas stop. Com passo −1 e ambos os limites None, indices(4) dá (3, -1, -1) para range, ou seja 3, 2, 1, 0 e o texto dcba.
O passo zero falha ao aplicar o 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"
Exemplos
selection = slice(None, None, -1)
print("abcd"[selection])
print(selection.indices(4))
Resultado
dcba (3, -1, -1)
O passo zero falha ao aplicar o corte.