开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜