@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 969 / 12833)
Slices are more forgiving of bad offsets than are single-index lookups. A slice offset earlier than the beginning of a string is treated as 0, and one after the end is treated as -1, as is demonstrated in this next series of examples.
>>> letters[-50:]
'abcdefghijklmnopqrstuvwxyz'
>>> letters[-51:-50]
''
>>> letters[:70]
'abcdefghijklmnopqrstuvwxyz'
>>> letters[70:71]
''
Je comprends ce qui précède.
one after the end is treated as -1
Je ne pense pas pouvoir trouver d'exemple.
Cela signifie-t-il qu'un après la fin est traité en réduisant de 1 (les indices qui dépassent la fin sont traités comme 1 moins)?
Recommended Posts