开发者

What happens when when the start value of a python substring command is negative?

开发者 https://www.devze.com 2023-03-01 01:16 出处:网络
Example: 开发者_开发知识库mystring = \" ... some string ... \" mystring[-50:40]# <--- what does this mean?

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

精彩评论

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