天天看点

3Python全栈之路系列之字符串数据类型

字符串类型是python的序列类型,他的本质就是字符序列,而且python的字符串类型是不可以改变的,你无法将原字符串进行修改,但是可以将字符串的一部分复制到新的字符串中,来达到相同的修改效果。

创建字符串类型可以使用单引号或者双引号又或者三引号来创建,实例如下:

单引号

<code>&gt;&gt; string </code><code>=</code> <code>'ansheng'</code>

<code># type是查看一个变量的数据类型</code>

<code>&gt;&gt;&gt; </code><code>type</code><code>(string)</code>

<code>&lt;</code><code>class</code> <code>'str'</code><code>&gt;</code>

双引号

<code>&gt;&gt;&gt; string </code><code>=</code> <code>"ansheng"</code>

<code>&gt;&gt;&gt; </code><code>type</code><code>(string) </code>

三引号

<code>&gt;&gt;&gt; string </code><code>=</code> <code>"""ansheng"""</code>

还可以指定类型

<code>&gt;&gt;&gt; var</code><code>=</code><code>str</code><code>(</code><code>"string"</code><code>)</code>

<code>&gt;&gt;&gt; var</code>

<code>'string'</code>

<code>&gt;&gt;&gt; </code><code>type</code><code>(var)</code>

每个类的方法其实都是很多的,无论我们在学习的过程中个还是工作的时候,常用的其实没有多少,所以我们没必要去可以得记那么多,有些方法我们只需要对其有个印象就ok了,忘了的时候可以google一下。

首字母变大写

capitalize(self):

<code>&gt;&gt;&gt; name</code><code>=</code><code>"ansheng"</code>

<code>&gt;&gt;&gt; name.capitalize()</code>

<code>'Ansheng'</code>

内容居中,width:字符串的总宽度;fillchar:填充字符,默认填充字符为空格。

center(self, width, fillchar=None):

<code># 定义一个字符串变量,名为"string",内容为"hello word"</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"hello word"</code>

<code># 输出这个字符串的长度,用len(value_name)</code>

<code>&gt;&gt;&gt; </code><code>len</code><code>(string)</code>

<code>10</code>

<code># 字符串的总宽度为10,填充的字符为"*"</code>

<code>&gt;&gt;&gt; string.center(</code><code>10</code><code>,</code><code>"*"</code><code>)</code>

<code>'hello word'</code>

<code># 如果设置字符串的总产都为11,那么减去字符串长度10还剩下一个位置,这个位置就会被*所占用</code>

<code>&gt;&gt;&gt; string.center(</code><code>11</code><code>,</code><code>"*"</code><code>)</code>

<code>'*hello word'</code>

<code># 是从左到右开始填充</code>

<code>&gt;&gt;&gt; string.center(</code><code>12</code><code>,</code><code>"*"</code><code>)</code>

<code>'*hello word*'</code>

统计字符串里某个字符出现的次数,可选参数为在字符串搜索的开始与结束位置。

count(self, sub, start=None, end=None):

参数

描述

sub

搜索的子字符串;

start

字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0;

end

字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置;

<code># 默认搜索出来的"l"是出现过两次的</code>

<code>&gt;&gt;&gt; string.count(</code><code>"l"</code><code>)</code>

<code>2</code>

<code># 如果指定从第三个位置开始搜索,搜索到第六个位置,"l"则出现过一次</code>

<code>&gt;&gt;&gt; string.count(</code><code>"l"</code><code>,</code><code>3</code><code>,</code><code>6</code><code>)</code>

<code>1</code>

解码

decode(self, encoding=None, errors=None):

<code># 定义一个变量内容为中文</code>

<code>temp </code><code>=</code> <code>"中文"</code>

<code># 把变量的字符集转化为UTF-8</code>

<code>temp_unicode </code><code>=</code> <code>temp.decode(</code><code>"utf-8"</code><code>)</code>

编码,针对unicode

encode(self, encoding=None, errors=None):

<code># 定义一个变量内容为中文,字符集为UTF-8</code>

<code>temp </code><code>=</code> <code>u</code><code>"中文"</code>

<code># 编码,需要指定要转换成什么编码</code>

<code>temp_gbk </code><code>=</code> <code>temp_unicode.encode(</code><code>"gbk"</code><code>)</code>

于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。

endswith(self, suffix, start=None, end=None):

suffix

后缀,可能是一个字符串,或者也可能是寻找后缀的tuple

开始,切片从这里开始

结束,片到此为止

<code># 判断字符串中是否已"d"结尾,如果是则返回"True"</code>

<code>&gt;&gt;&gt; string.endswith(</code><code>"d"</code><code>)</code>

<code>True</code>

<code># 判断字符串中是否已"t"结尾,不是则返回"False"</code>

<code>&gt;&gt;&gt; string.endswith(</code><code>"t"</code><code>)</code>

<code>False</code>

<code># 制定搜索的位置,实则就是从字符串位置1到7来进行判断,如果第七个位置是"d",则返回True,否则返回False</code>

<code>&gt;&gt;&gt; string.endswith(</code><code>"d"</code><code>,</code><code>1</code><code>,</code><code>7</code><code>)</code>

把字符串中的tab符号(‘\t’)转为空格,tab符号(‘\t’)默认的空格数是8。

expandtabs(self, tabsize=None):

tabsize

指定转换字符串中的 tab 符号(‘\t’)转为空格的字符数

<code>&gt;&gt;&gt; string</code><code>=</code><code>"hello       word"</code>

<code># 输出变量"string"内容的时候会发现中间有一个"\t",这个其实就是一个`tab`键</code>

<code>&gt;&gt;&gt; string</code>

<code>'hello\tword'</code>

<code># 把`tab`键换成一个空格</code>

<code>&gt;&gt;&gt; string.expandtabs(</code><code>1</code><code>)</code>

<code># 把`tab`键换成十个空格</code>

<code>&gt;&gt;&gt; string.expandtabs(</code><code>10</code><code>)</code>

<code>'hello     word'</code>

检测字符串中是否包含子字符串str,如果指定beg(开始)和end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

find(self, sub, start=None, end=None):

str

指定检索的字符串

beg

开始索引,默认为0

结束索引,默认为字符串的长度

<code># 返回`o`在当前字符串中的位置,如果找到第一个`o`之后就不会再继续往下面寻找了</code>

<code>&gt;&gt;&gt; string.find(</code><code>"o"</code><code>)</code>

<code>4</code>

<code># 从第五个位置开始搜索,返回`o`所在的位置</code>

<code>&gt;&gt;&gt; string.find(</code><code>"o"</code><code>,</code><code>5</code><code>)</code>

<code>7</code>

字符串格式,后续文章会提到。

format(args, *kwargs):

检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

index(self, sub, start=None, end=None):

<code># 返回字符串所在的位置</code>

<code>&gt;&gt;&gt; string.index(</code><code>"o"</code><code>)</code>

<code># 如果查找一个不存在的字符串那么就会报错</code>

<code>&gt;&gt;&gt; string.index(</code><code>"a"</code><code>)</code>

<code>Traceback (most recent call last):</code>

<code>  </code><code>File</code> <code>"&lt;stdin&gt;"</code><code>, line </code><code>1</code><code>, </code><code>in</code> <code>&lt;module&gt;</code>

<code>ValueError: substring </code><code>not</code> <code>found</code>

法检测字符串是否由字母和数字组成,如果string至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False

isalnum(self):

<code>&gt;&gt;&gt; string</code><code>=</code><code>"hes2323"</code>

<code># 如果存在数字或字母就返回`True`,否则返回`False`</code>

<code>&gt;&gt;&gt; string.isalnum()</code>

<code># 中间有空格返回的就是False了</code>

检测字符串是否只由字母组成。

isalpha(self):

<code># 如果全部都是字母就返回`True`</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"helloword"</code>

<code>&gt;&gt;&gt; string.isalpha()</code>

<code># 否则就返回False</code>

检测字符串是否只由数字组成

isdigit(self):

<code># 如果变量里面都是数字就返回`True`,否则就返回`False`</code>

<code>&gt;&gt;&gt; string.isdigit()</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"2323"</code>

检测字符串是否由小写字母组成

islower(self):

<code># 如果变量内容全部都是小写字母就返回`True`,否则就返回`False`</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"hesasdasd"</code>

<code>&gt;&gt;&gt; string.islower()</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"HelloWord"</code>

检测字符串是否只由空格组成

isspace(self):

<code># 如果变量内容由空格来组成,那么就返回`True`否则就返回`False`</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>" "</code>

<code>&gt;&gt;&gt; string.isspace()</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"a"</code>

检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

istitle(self):

<code># 如果变量的内容首字母是大写并且其他字母为小写,那么就返回`True`,否则会返回`False`</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"Hello Word"</code>

<code>&gt;&gt;&gt; string.istitle()</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"Hello word"</code>

检测字符串中所有的字母是否都为大写。

isupper(self):

<code># 如果变量值中所有的字母都是大写就返回`True`,否则就返回`False`</code>

<code>&gt;&gt;&gt; string.isupper()</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"HELLO WORD"</code>

将序列中的元素以指定的字符连接生成一个新的字符串。

join(self, iterable):

<code>&gt;&gt;&gt; string</code><code>=</code><code>(</code><code>"a"</code><code>,</code><code>"b"</code><code>,</code><code>"c"</code><code>)</code>

<code>&gt;&gt;&gt; </code><code>'-'</code><code>.join(string)</code>

<code>'a-b-c'</code>

返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。

ljust(self, width, fillchar=None):

width

指定字符串长度

fillchar

填充字符,默认为空格

<code>&gt;&gt;&gt; string</code><code>=</code><code>"helo word"</code>

<code>9</code>

<code># 定义的长度减去字符串的长度,剩下的就开始填充</code>

<code>&gt;&gt;&gt; string.ljust(</code><code>15</code><code>,</code><code>'*'</code><code>)</code>

<code>'helo word******'</code>

转换字符串中所有大写字符为小写。

lower(self):

<code># 把变量里的大写全部转换成小写</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"Hello WORD"</code>

<code>&gt;&gt;&gt; string.lower()</code>

截掉字符串左边的空格或指定字符

lstrip(self, chars=None):

chars

指定截取的字符

<code># 从左侧开始删除匹配的字符串</code>

<code>&gt;&gt;&gt; string.lstrip(</code><code>"hello "</code><code>)</code>

<code>'word'</code>

用来根据指定的分隔符将字符串进行分割,如果字符串包含指定的分隔符,则返回一个3元的tuple,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

partition(self, sep):

指定的分隔符

<code># 返回的是一个元组类型</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"www.ansheng.me"</code>

<code>&gt;&gt;&gt; string.partition(</code><code>"ansheng"</code><code>)</code>

<code>(</code><code>'www.'</code><code>, </code><code>'ansheng'</code><code>, </code><code>'.me'</code><code>)</code>

把字符串中的 old(旧字符串)替换成new(新字符串),如果指定第三个参数max,则替换不超过max次

replace(self, old, new, count=None):

old

将被替换的子字符串

new

新字符串,用于替换old子字符串

count

可选字符串, 替换不超过count次

<code># 把就字符串`www.`换成新字符串`https://`</code>

<code>&gt;&gt;&gt; string.replace(</code><code>"www."</code><code>,</code><code>"https://"</code><code>)</code>

<code>'https://blog.ansheng.me'</code>

<code># 就字符串`w`换成新字符串`a`只替换`2`次</code>

<code>&gt;&gt;&gt; string.replace(</code><code>"w"</code><code>,</code><code>"a"</code><code>,</code><code>2</code><code>)</code>

<code>'aaw.ansheng.me'</code>

返回字符串最后一次出现的位置,如果没有匹配项则返回-1。

rfind(self, sub, start=None, end=None):

查找的字符串

开始查找的位置,默认为0

结束查找位置,默认为字符串的长度

<code># rfind其实就是反向查找</code>

<code>&gt;&gt;&gt; string.rfind(</code><code>"o"</code><code>)</code>

<code># 指定查找的范围</code>

<code>&gt;&gt;&gt; string.rfind(</code><code>"o"</code><code>,</code><code>0</code><code>,</code><code>6</code><code>)</code>

返回子字符串str在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数<code>[beg:end]</code>设置查找的区间。

rindex(self, sub, start=None, end=None):

<code># 反向查找索引</code>

<code>&gt;&gt;&gt; string.rindex(</code><code>"o"</code><code>)</code>

<code># 如果没有查找到就报错</code>

<code>&gt;&gt;&gt; string.rindex(</code><code>"a"</code><code>)</code>

返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。

rjust(self, width, fillchar=None):

指定填充指定字符后中字符串的总长度

填充的字符,默认为空格

<code>&gt;&gt;&gt; string.rjust(</code><code>10</code><code>,</code><code>"*"</code><code>)</code>

<code>&gt;&gt;&gt; string.rjust(</code><code>12</code><code>,</code><code>"*"</code><code>)</code>

<code>'**hello word'</code>

从右到左通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串

rsplit(self, sep=None, maxsplit=None):

隔符,默认为空格

num

分割次数

<code>&gt;&gt;&gt; string.rsplit(</code><code>"."</code><code>,</code><code>1</code><code>)</code>

<code>[</code><code>'www.ansheng'</code><code>, </code><code>'me'</code><code>]</code>

<code>&gt;&gt;&gt; string.rsplit(</code><code>"."</code><code>,</code><code>2</code><code>)</code>

<code>[</code><code>'www'</code><code>, </code><code>'ansheng'</code><code>, </code><code>'me'</code><code>]</code>

删除string字符串末尾的指定字符(默认为空格).

rstrip(self, chars=None):

指定删除的字符

<code># 从尾部开始匹配删除</code>

<code>&gt;&gt;&gt; string.rstrip(</code><code>"d"</code><code>)</code>

<code>'hello wor'</code>

从左到右通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串

split(self, sep=None, maxsplit=None):

分隔符,默认为空格

<code># 指定切一次,以`.`来分割</code>

<code>&gt;&gt;&gt; string.split(</code><code>"."</code><code>,</code><code>1</code><code>)</code>

<code>[</code><code>'www'</code><code>, </code><code>'ansheng.me'</code><code>]</code>

<code># 指定切二次,以`.`来分割</code>

<code>&gt;&gt;&gt; string.split(</code><code>"."</code><code>,</code><code>2</code><code>)</code>

按照行分隔,返回一个包含各行作为元素的列表,如果num指定则仅切片num个行.

splitlines(self, keepends=False):

分割行的次数

<code># 定义一个有换行的变量,`\n`可以划行</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"www\nansheng\nme"</code>

<code># 输出内容</code>

<code>&gt;&gt;&gt; </code><code>print</code><code>(string)</code>

<code>www</code>

<code>ansheng</code>

<code>me</code>

<code># 把有行的转换成一个列表</code>

<code>&gt;&gt;&gt; string.splitlines(</code><code>1</code><code>)</code>

<code>[</code><code>'www\n'</code><code>, </code><code>'ansheng\n'</code><code>, </code><code>'me'</code><code>]</code>

检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。

startswith(self, prefix, start=None, end=None):

检测的字符串

strbeg

可选参数用于设置字符串检测的起始位置

strend

可选参数用于设置字符串检测的结束位置

<code>&gt;&gt;&gt; string.startswith(</code><code>"www"</code><code>)</code>

<code>&gt;&gt;&gt; string.startswith(</code><code>"www"</code><code>,</code><code>3</code><code>)</code>

移除字符串头尾指定的字符(默认为空格)

strip(self, chars=None):

移除字符串头尾指定的字符

<code>&gt;&gt;&gt; string</code><code>=</code><code>" www.ansheng.me "</code>

<code>' www.ansheng.me '</code>

<code># 删除空格</code>

<code>&gt;&gt;&gt; string.strip()</code>

<code>'www.ansheng.me'</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"_www.ansheng.me_"</code>

<code># 指定要把左右两边的"_"删除掉</code>

<code>&gt;&gt;&gt; string.strip(</code><code>"_"</code><code>)</code>

用于对字符串的大小写字母进行转换,大写变小写,小写变大写

swapcase(self):

<code>&gt;&gt;&gt; string</code><code>=</code><code>"hello WORD"</code>

<code>&gt;&gt;&gt; string.swapcase()</code>

<code>'HELLO word'</code>

返回”标题化”的字符串,就是说所有单词都是以大写开始,其余字母均为小写。

title(self):

<code>&gt;&gt;&gt; string.title()</code>

<code>'Hello Word'</code>

根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。

translate(self, table, deletechars=None):

table

翻译表,翻译表是通过maketrans方法转换而来

deletechars

字符串中要过滤的字符列表

将字符串中的小写字母转为大写字母

upper(self):

<code>&gt;&gt;&gt; string.upper()</code>

<code>'HELLO WORD'</code>

返回指定长度的字符串,原字符串右对齐,前面填充0

zfill(self, width):

指定字符串的长度。原字符串右对齐,前面填充0

<code>&gt;&gt;&gt; string.zfill(</code><code>10</code><code>)</code>

<code>&gt;&gt;&gt; string.zfill(</code><code>20</code><code>)</code>

<code>'0000000000hello word'</code>

以UTF-8编码的时候,一个汉字是三个字节,一个字节是八位

3.5.x实例

代码如下:

<code>#!/usr/bin/env python</code>

<code># _*_ coding:utf-8 _*_</code>

<code>var </code><code>=</code> <code>"中文"</code>

<code>for</code> <code>n </code><code>in</code> <code>var:</code>

<code>    </code><code>print</code><code>(n)</code>

<code>print</code><code>(</code><code>"================"</code><code>)</code>

<code>var2 </code><code>=</code> <code>"zhongwen"</code>

<code>for</code> <code>n </code><code>in</code> <code>var2:</code>

执行结果:

<code>C:\Python35\python.exe F:</code><code>/</code><code>Python_code</code><code>/</code><code>sublime</code><code>/</code><code>Day03</code><code>/</code><code>str</code><code>.py</code>

<code>中</code>

<code>文</code>

<code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code><code>=</code>

<code>z</code>

<code>h</code>

<code>o</code>

<code>n</code>

<code>g</code>

<code>w</code>

<code>e</code>

2.7.x实例

执行结果

<code>C:\Python27\python.exe F:</code><code>/</code><code>Python_code</code><code>/</code><code>sublime</code><code>/</code><code>Day03</code><code>/</code><code>str</code><code>.py</code>

通过上面的实例可以知道,<code>Python3.5.x</code>在输出中文或者英文的时候是按照一个字符一个字符来输出的,但是在<code>Python2.7.x</code>就不这样了,<code>Python2.7.x</code>是按照字节来进行输出的,可以看到在输出中文的时候是乱码的,而且还输出了六次,因为在UTF-8编码的情况下一个汉字是等于三个字节的,所以输出了六个乱码的字符。

在Python3.5.x里面是既可以输出汉字,也可以把输出字节的,利用bytes这个方法,bytes可以将字符串转换为字节

<code>var</code><code>=</code><code>"中文"</code>

<code>    </code><code>bytes_list </code><code>=</code> <code>bytes(n, encoding</code><code>=</code><code>'utf-8'</code><code>)</code>

<code>    </code><code># 十六进制输出</code>

<code>    </code><code>print</code><code>(bytes_list)</code>

<code>    </code><code>for</code> <code>x </code><code>in</code> <code>bytes_list:</code>

<code>        </code><code># 十进制,bin(x)二进制</code>

<code>        </code><code>print</code><code>(x,</code><code>bin</code><code>(x))</code>

输出的结果

<code># 字符串</code>

<code># 十六进制</code>

<code>b</code><code>'\xe4\xb8\xad'</code>

<code># 228=十进制,0b11100100=二进制</code>

<code>228</code> <code>0b11100100</code>

<code>184</code> <code>0b10111000</code>

<code>173</code> <code>0b10101101</code>

<code>b</code><code>'\xe6\x96\x87'</code>

<code>230</code> <code>0b11100110</code>

<code>150</code> <code>0b10010110</code>

<code>135</code> <code>0b10000111</code>

b代表十六进制,\xe4这样的是一个十六进制的字节

索引是指某个值在列表或别的数据类型中的一个位置

定义一个列表,查看列表中<code>Linux</code>值对应在列表中的位置

<code>&gt;&gt;&gt; list_os </code><code>=</code> <code>[</code><code>"Windows"</code><code>,</code><code>"Linux"</code><code>,</code><code>"Mac"</code><code>,</code><code>"Unix"</code><code>]</code>

<code>&gt;&gt;&gt; list_os.index(</code><code>"Linux"</code><code>)</code>

<code>&gt;&gt;&gt; list_os[</code><code>1</code><code>]</code>

<code>'Linux'</code>

Python允许你对某些字符进行转义,以此来实现一些难以单纯用字符描述的效果

<code># 常用的内容也转义也就是`\n`和`\t`了,`\n`是用来换行的,`\t`是用来代替一个`tab`键</code>

<code>&gt;&gt;&gt; string</code><code>=</code><code>"My \n Name  \t is"</code>

<code>My</code>

<code> </code><code>Name    </code><code>is</code>

你可以使用<code>+</code>号将多个字符串或字符串变量拼接起来

<code>&gt;&gt;&gt; a</code><code>=</code><code>"my "</code>

<code>&gt;&gt;&gt; b</code><code>=</code><code>"name "</code>

<code>&gt;&gt;&gt; c</code><code>=</code><code>"is "</code>

<code>&gt;&gt;&gt; d</code><code>=</code><code>"ansheng"</code>

<code>&gt;&gt;&gt; a</code><code>+</code><code>b</code><code>+</code><code>c</code><code>+</code><code>d</code>

<code>'my name is ansheng'</code>

切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。注意这与你使用的索引操作符十分相似。记住数是可选的,而冒号是必须的,切片操作符中的第一个数表示切片开始的位置,第二个数表示切片到哪里结束,第三个数表示切片间隔数。如果不指定第一个数,Python就从序列首开始。如果没有指定第二个数,则Python会停止在序列尾。注意,返回的序列从开始位置开始 ,刚好在结束位置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。

<code>&gt;&gt;&gt; os</code><code>=</code><code>"Linux"</code>

<code>&gt;&gt;&gt; os</code>

<code>&gt;&gt;&gt; os[</code><code>0</code><code>:</code><code>2</code><code>]</code>

<code>'Li'</code>

<code>&gt;&gt;&gt; os[</code><code>0</code><code>:</code><code>4</code><code>:</code><code>2</code><code>]</code>

<code>'Ln'</code>

更多实例如下

切片符

说明

[:]

提取从开头到结尾的整个字符串

[start:]

从start到结尾的字符串

[:end]

从开头提取到end - 1

[start:end]

从start提取到end - 1

[start : end : setp]

从start提取到end-1,每setp个字符提取一个

索引和切片同时适用于字符串、列表与元组

索引通常用于查找某一个字符串或值

切片通常用于查找某一个范围内的字符串或值

实例:

<code># 定义一个列表,列表内有三个元素</code>

<code>&gt;&gt;&gt; var</code><code>=</code><code>[</code><code>"Linux"</code><code>,</code><code>"Win"</code><code>,</code><code>"Unix"</code><code>]</code>

<code># 通过索引取到了一个值</code>

<code>&gt;&gt;&gt; var[</code><code>0</code><code>]</code>

<code># 通过切片取到了多个值</code>

<code>&gt;&gt;&gt; var[</code><code>0</code><code>:</code><code>2</code><code>]</code>

<code>[</code><code>'Linux'</code><code>, </code><code>'Win'</code><code>]</code>

<code>&gt;&gt;&gt; var[</code><code>1</code><code>:</code><code>3</code><code>]</code>

<code>[</code><code>'Win'</code><code>, </code><code>'Unix'</code><code>]</code>

本文转自 Edenwy  51CTO博客,原文链接:http://blog.51cto.com/edeny/1910785,如需转载请自行联系原作者