Python-切片

#list切片
L1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print 'L1[0:3] =', L1[0:3]
print 'L1[-5:-2] =', L1[-5:-2]字符串

#tuple也是一種list,惟一區別是tuple不可變。所以,tuple也能夠用切片操做,只是操做的結果還是tuple:
e1 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
print 'e1[0:3] =', e1[0:3]
print 'e1[:] =', e1[:]字符

#字符串切片 str = "ABCDEFG" print 'str[0:3] =', str[0:3] print 'str[:-3] =', str[:-3]

相關文章
相關標籤/搜索