What happens when when the start value of a python substring command is negative?
Example:
开发者_开发知识库mystring = " ... some string ... "
mystring[-50:40] # <--- what does this mean?
Negative indexes in a slice count from the right. Yes, even for the start value.
>>> '12345'[-4:4]
'234'
Negative indexes in general have an implied len(of_this_object)
added to the negative index. Doesn't matter if they are start, end or plain indexes.
Example:
some_object[10:-20]
is interpreted as meaning:
some_object[10:len(some_object)-20]
This is why:
some_object[-1]
means the last item in some_object
.
精彩评论