+ 1
How this works
Python strings https://code.sololearn.com/cfB8D44spwh1/?ref=app
3 Answers
+ 1
string can used be a list
See:
https://www.sololearn.com/Discuss/2955122/can-someone-explain-this-string-slicing
More deep:
https://docs.python.org/2/reference/datamodel.html#object.__getslice__
https://www.python.org/dev/peps/pep-0204/
https://github.com/python/cpython/blob/main/Objects/sliceobject.c
+ 1
python
We can try to convert the source code to python, like this:
def PySlice_AdjustIndices(length, start, stop, step):
assert step != 0
assert step >= -2**64
if (start < 0):
start += length
if (start < 0):
start = (step < 0) and -1 or 0
elif (start >= length):
start = (step < 0) and length - 1 or length
if (stop < 0):
stop += length
if (stop < 0):
stop = (step < 0) and -1 or 0
elif (stop >= length):
stop = (step < 0) and length - 1 or length
if (step < 0):
if (stop < start):
return (start - stop - 1) // (-step) + 1
else:
if (start < stop):
return (stop - start - 1) // step + 1
return 0
a = "Hello Everyone"
print(PySlice_AdjustIndices(len(a), 0, -100, -1))
0
đđđš thank you