标签:
1 #列表,元组,字符串都是序列,序列常用操作为分片 2 item = (‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘) #元组 3 list = [‘apple‘, ‘mango‘, ‘carrot‘, ‘banana‘] #列表 4 5 # Indexing or ‘Subscription‘ operation 6 7 #取指定的一个片段,位置可以顺序0、1...或倒序...-2、-1 8 print (‘list 0 is‘, list[0]) 9 print (‘list 1 is‘, list[1]) 10 print (‘list 2 is‘, list[2]) 11 print (‘list 3 is‘, list[3]) 12 print (‘list -1 is‘, list[-1]) 13 print (‘list -2 is‘, list[-2]) 14 print (‘list 0 is %s,and item 1 is %s‘ % (list[0],item[1])) 15 16 # Slicing on a list or item 17 print(‘item [0,2] is ‘,item[0:2]) #用:分割开始序号和结束位置(不包括),分号不可省 18 print(‘item [0,-1] is ‘,item[0:-1]) #开始和结束序号都可为负数,负数为倒序 19 print(‘item [ :3] is ‘,list[:3]) #省略开始,默认从0, 20 print(‘item [1: ] is ‘,list[1:]) #省略结束,默认到结尾(包含), 21 print(‘item [ : ] is ‘,list[:]) #分割开始序号和结束位置都可省略,取得全部元素 22 23 # Slicing on a string 24 name = ‘swaroop‘ 25 print (‘Characters [1:3] is ‘,name[1:3]) 26 print (‘Characters [1:3] is %s‘ % name[1:3]) #注意区别与上面print语句的分别 27 print (‘Characters [0:4] is‘,name[0:4]) 28 print (‘Characters [:5] is‘,name[:5]) 29 print (‘Characters [2:] is‘,name[2:]) 30 print (‘Characters [1:-1] is‘,name[1:-1]) 31 print (‘Characters [:] is‘,name[:])
标签:
原文地址:http://www.cnblogs.com/Miky-Mi/p/4694413.html