Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages

Python indexes and slices for a six-element list.
Indexes enumerate the elements, slices enumerate the spaces between the elements. Index from rear: -6 -5 -4 -3 -2 -1 a=[0,1,2,3,4,5] a[1:]==[1,2,3,4,5]
Index from front: 0 1 2 3 4 5 len(a)==6 a[:5]==[0,1,2,3,4]
+---+---+---+---+---+---+ a[0]==0 a[:-2]==[0,1,2,3]
| a | b | c | d | e | f | a[5]==5 a[1:2]==[1]
+---+---+---+---+---+---+ a[-1]==5 a[1:-1]==[1,2,3,4]
Slice from front: : 1 2 3 4 5 : a[-2]==4
Slice from rear: : -5 -4 -3 -2 -1 :
b=a[:]
b==[0,1,2,3,4,5] (shallow copy of a)

In python 2.7

Slicing in python

[a:b:c]

len = length of string, tuple or list

c -- default is +1. sign of c indicates forward or backward, absolute value of c indicates steps. Default is forward with step size 1. Positive means forward, negative means backward.

a -- when c is positive or blank, default is 0. when c is negative, default is -1.

b -- when c is positive or blank, default is len. when c is negative, default is -(len+1).

Understanding index assignment is very important.

In forward direction, starts at 0 and ends at len-1

In backward direction, starts at -1 and ends at -len

when you say [a:b:c] you are saying depending on sign of c (forward or backward), start at a and end at b ( excluding element at bth index). Use the indexing rule above and remember you will only find elements in this range

-len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1

but this range continues in both directions infinitely

...,-len -2 ,-len-1,-len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1, len, len +1, len+2 , ....

e.g.

             0    1    2   3    4   5   6   7   8   9   10   11
a s t r i n g
-9 -8 -7 -6 -5 -4 -3 -2 -1

if your choice of a , b and c allows overlap with the range above as you traverse using rules for a,b,c above you will either get a list with elements (touched during traversal) or you will get an empty list.

One last thing: if a and b are equal , then also you get an empty list

>>> l1
[2, 3, 4] >>> l1[:]
[2, 3, 4] >>> l1[::-1] # a default is -1 , b default is -(len+1)
[4, 3, 2] >>> l1[:-4:-1] # a default is -1
[4, 3, 2] >>> l1[:-3:-1] # a default is -1
[4, 3] >>> l1[::] # c default is +1, so a default is 0, b default is len
[2, 3, 4] >>> l1[::-1] # c is -1 , so a default is -1 and b default is -(len+1)
[4, 3, 2] >>> l1[-100:-200:-1] # interesting
[] >>> l1[-1:-200:-1] # interesting
[4, 3, 2] >>> l1[-1:5:1]
[4] >>> l1[-1:-1:1]
[] >>> l1[-1:5:1] # interesting
[4] >>> l1[1:-7:1]
[] >>> l1[1:-7:-1] # interesting
[3, 2]


Python学习--字符串slicing的更多相关文章

  1. python学习--字符串

    python的字符串类型为str 定义字符串可以用 ‘abc' , "abc", '''abc''' 查看str的帮助 在python提示符里 help(str) python基于 ...

  2. python学习-字符串 列表 元祖

    目录 Python翻转字符串(reverse string) 简单的步长为-1, 即字符串的翻转(常用) 递归反转 借用列表,使用reverse()方法 字符串常用操作 index split 切片 ...

  3. python学习——字符串

    1)字符串解释 字符串是python中常用的数据类型我们可以使用" "或' '来创建字符串. 2)字符串操作 """访问字符串中的值"&qu ...

  4. python学习-字符串前面添加u,r,b的含义

    引用:https://www.cnblogs.com/cq90/p/6959567.html u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unico ...

  5. Python学习---字符串处理

    This world is but a canvas to our imagination. 世界只是我们的想象的画布. ----Apri 22 ''' 题目内容: "Pig Latin&q ...

  6. Python学习---字符串操作

    ### 截取字符串然后拼接 str = "Hello World!" str2 = str[:6] + "tyche !" print(str2) ===> ...

  7. Python学习-字符串的基本知识

    字符串的基本知识 根据所展示形式的不同,字符串也可以分为两类 原始字符串: 使用单引号包括:‘liuwen’ 使用双引号包括:"liuwen" 使用3个单引号包括 :'''liuw ...

  8. Python学习-字符串函数操作3

    字符串函数操作 isprintable():判断一个字符串中所有字符是否都是可打印字符的. 与isspace()函数很相似 如果字符串中的所有字符都是可打印的字符或字符串为空返回 True,否则返回 ...

  9. Python学习-字符串函数操作2

    字符串函数操作 find( sub, start=None, end=None):从左到右开始查找目标子序列,找到了结束查找返回下标值,没找到返回 -1 sub:需要查找的字符串 start=None ...

随机推荐

  1. css小贴士备忘录

    前言:在CSS的学习实践过程中,我经常遗忘一些貌似常用的代码,为了能够强化记忆特在此作归纳整理并将陆续增删,以备即时查阅.但愿今后能遇到问题及时解决,牢牢记住这些奇怪的字符们. 一.关于段落文本强制对 ...

  2. ryu学习笔记(2) 之 ryu-manager运行报错

    http://blog.csdn.net/haimianxiaojie/article/details/48769653 ryu在使用的时候最常出现的报错是:address already in us ...

  3. Build 2016上一些令人兴奋的东西

    本来应该要更新Windows IOT开发入门的,但是由于Build 2016刚刚过去,不得不将一些令人兴奋的东西给大家进行分享. 首先对于错过这次直播的,可以在以下链接再次看到观看: https:// ...

  4. php命令行脚本 mock数据

    <?php $con = mysql_connect("192.168.1.5:3306","root","123"); if (!$ ...

  5. 关于Unity中的本地存储

    本地存储 在做游戏的时候,经常需要在本机存储一些数据,比如闯关类游戏要记录闯到第几关,做单机的时候要把数据保存到本地,下次启动的时候数据存在,就是把数据保存到磁盘里面或者手机的flash闪存里面. U ...

  6. 【WPF】创建文本字符串的路径PathGeometry

    /// <summary> /// 创建文本路径 /// </summary> /// <param name="word">文本字符串< ...

  7. c# 正则表达式 首字母转大写

    class Program { static void Main(string[] args) { // Input strings. const string s1 = "samuel a ...

  8. C#常用数据类型间的转换

    数据类型有很多种,数据类型间的转换也是有很多的方法,如果不细心整理的话等到用的时候再查就会显得很浪费时间,所以决心整理出这篇博文.主要是讲解常用数据类型之间的转换方法以及常见数据类型所占字节数. 字节 ...

  9. LigerUI可编辑表格左下角出现白色小方块遮罩层问题解决办法

    LigerUI已经研究了一段时间,总体感觉还不错,基于jQuery开发,框架提供了丰富的UI组件,尤其LigerUI表格,功能还是挺强大的 在使用LigerUI可编辑表格的时候,发现一个小小的问题 当 ...

  10. 调整iRedmail之Roundcube webmail服务不可不知的几件事

    iRedMail集成了roundcube webmail,极大的方便了安装维护工作,但是需求是千遍万化的,总有需要深入调整的地方.下面就是我调整roundcube时遇到的几点问题: 一.修改网页标题p ...