a = [1,2,3,4,5,6,7,8,9,10]
a[0:1] = [1] a[0:2] = [1,2]
a[0:1]
[1]
a[0:2]
[1,2]
包含开头,不包含结尾。
a [:-1]: 从头一直到最后一个元素a[-1],但不包含最后一个元素。
a [:-1]
a[-1]
Python这样做的原因是:
word[:2] # 取前两个 word[2:] # 除了前两个,其他全部选取
word[:2] # 取前两个
word[2:] # 除了前两个,其他全部选取