序列类型是其元素被顺序放置的一种数据结构类型,这种方式允许通过下标的方式来获得某一个数据元素,或者通过指定下标范围来获得一组序列的元素。这种访问序列的方式叫做切片。字符串也可以使用切片操作。切片操作符:[] [:] [::],调用内置函数slice()函数。
以字符串'abcdefg'为例:
s |
a |
b |
c |
d |
e |
f |
g |
index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
index |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
一、sequence[index]
类似于其他语言的数组操作。sequence是序列的名字,index是想要访问的元素对应的偏移量。偏移量正负都可以,-len(sequence)<=index<=len(sequence)-1。正索引以序列的开始为起点,负索引以序列的结束为起点。
试图访问一个越界的索引会引发一个IndexError异常。
二、sequence[starting_index:ending_index]
通过这种方式可以得到从起始索引到结束索引(不包括结束索引所对应的元素)之间的元素,起始索引和结束索引都是可选的,如果没有提供或者用None作为索引值,切片操作会从序列的最开始处开始,或者直到序列的最末尾结束。其中,开始和结束的索引值可以超出字符串长度,如:s[-100:100],返回'abcdefg'.
用一个:时,starting_index应该小于ending_index,否则返回空字符串。
# 返回整个序列,即 'abcdefg'
s
s[:] # 返回 'abc'
s[0:3]
s[:3] # 返回 'cde'
s[2:5]
s[:5] # 返回 'def'
s[-4:-1]
三、sequence[starting_index:ending_index:step]
扩展切片操作,step为步长参数,类似range()里的步长参数。
得到的序列从starting_index(包含starting_index)开始,每次以步长前进,即starting_index + step,直到ending_index结束。
执行:
# 返回 'fedc'
s = 'abcdefg'
s[-2:-6:-1]
(1) starting_index = -2 ending_index = -6 step = -1
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvUAAACYCAIAAADr1YF3AAAO70lEQVR4nO3dz44j1RXHcYuQBSs2PAcPYN6BFTu2SYEEz0CUbbLAi0T5ww40ZIUESuQlYhIhwQIhhJBmMMwCDdIwQcyMGA2CyczNouzje6tcbru73Od3bn0/q3S7bW5+Xff45yq7Z5YAAADqMvNeAAAAwMjoNwAAoDb0GwAAUBv6DQAAqA39BgA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tpQy2a7j3tvuOt89mDz9fok8GC/2fsiauT1Y2yEDkXHXG7PBlI24Ib7TTveeH8xEMaBG2rgw5Tb75cflNp+fnKw3/Tve8r1Y1yEDkWHj4PiTThl1dnzOc/iY+OrxZx+AwiLvqGirz8oQoei6OMg+voBKdE3VPT1B0XoUBR9HERfPyAl+oaKvv6gCB2Koo+D6OsHpETfUNHXHxShQ1H0cRB9/YCU6Bsq+vqDInQoij4Ooq8fkBJ9Q0Vff1CEDkXRx0H09QNSom+o6OsPitChKPo4iL5+QEr0DRV9/UEROhRFHwfR1w9Iib6hoq8/KEKHoujjIPr6ASnRN1T09QdF6FAUfRxEXz8gJfqGir7+oAgdiqKPg+jrB6RE31DR1x8UoUNR9HEQff2AlOgbKvr6gyJ0KIo+DqKvH5ASfUNFX39QhA5F0cdB9PUDUmbxeUc4RYQORd6zaATeEQLApDGFAQBAbeg3AACgNvQbAABQG/oNAACoDf0GAADUhn4DAABqQ78BAAC1od8AAIDa0G8AAEBt6DcAAKA2/weU+8MrpgvbiwAAAABJRU5ErkJggg==" alt="" width="674" height="116" />
(2) 第一个读sequence[-2],即 'f',第二个读starting_index + step,即 -2+(-1)=-3,sequence[-3],也就是 'e'。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvkAAACXCAIAAAAAv9MsAAAPYUlEQVR4nO3dTc8b1RnGcYvSBSs2fA4+gPkOrNixbQek8Bmoum0XeNGqL+xAoSskUKtZItIKCRYIIYSUYMgCBekhRSQRURBJk9PFsW+fOePxM/ac8X2fc/6/Fc+L7eHyPceXZ8ZPFg4AAKBcC+0NAAAAmBFdBwAAlIyuAwAASkbXAQAAJaPrAACAktF1AABAyeg6AACgZHQdAABQMroOAAAoGV0HAACUjK4DAABKRtcBAAAlo+sAAICS0XUAAEDJ6DoAAKBkdB0AAFAyug4AACjZpK6zAAAASCRVuYnryqQbz7ZZ2SEKQRSCKDxyEEQhiEIQhaDrWEcUgigEUXjkIIhCEIUgCkHXsY4oBFEIovDIQ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Yy50GF6CBz6Z0jbx2kzXwRxGjsHARwl33+9/6tt/enCw6/RvO/nRO2eMo2uBlqv1wAcep2NXQipMEswZv8BdsgS7/V93Vubxf2BtNBZouDrGYO4/OlhDhjgPJgnm5L7A5b79SKKGMaDrIBdMEszJfYHLffuRRA1jQNdBLpgkmJP7Apf79iMJxmA6MkQqTBLMyX2By337kQRjMB0ZIhUmCebkvsDlvv1IgjGYjgyRCpMEc3Jf4HLffiTBGExHhkiFSYI5uS9wuW8/kmAMpiNDpMIkwZzcF7jctx9JMAbTkSFSYZJgTu4LXO7bjyQYg+nIEKkwSTAn9wUu9+1HEozBdGSIVJgkmJP7Apf79iMJxmA6MkQqTBLMyX2By337kQRjMB0ZIhUmCebkvsDlvv1IgjGYjgyRCpMEc3Jf4HLffiSxQAraTyMKwSTBHO3VNQHtCAEAOyzKAACgZHQdAABQMroOAAAoGV0HAACUjK4DAABKRtcBAAAlo+sAAICS0XUAAEDJ/g+2W5sS2osQ5AAAAABJRU5ErkJggg==" alt="" width="657" height="110" />
(3) 按这个顺序继续读,不包括ending_index,即'b',所以s[-2:-6:-1]返回 'fedc'。
同理,输出s[2:5:2],返回 'ce'。
有一个经常用到的应用:翻转字符串
# 输出 'gfedcba'
1 s =s'abcdefg'
print s[::-1]