欢迎访问mehome的博客

Tomorrow is another day.
Fork me on GitHub

序列内置方法详解(string/list/tuple)

一、常用方法集合

1.1、string,字符串常用方法

以下举例是python2.7测试:

函数名称

作用

举例

str.capitalize()

字符串第一个字符如果是字母,则把字母替换为大写字母。然后返回新的字符串

>>> l = 'abc cba'

>>> l.capitalize()

'Abc cba'

>>> l.capitalize()

'2b 3c'

str.center()

字符串设置为指定字符宽度,并居中。默认是用空格填充前后空白字符。可以用第二个参数指定填充字符

>>> s = 'test'

>>> s.center(10)

'   test   '

>>> s.center(10,'=')

'===test==='

>>> s.center(9,'=')

'===test=='

str.count()

统计字符串中重复次数,返回整数次数

>>> s = 'test abcde'

>>> s.count('e')

2

>>> s.count('e', 1)

2

>>> s.count('e', 2)

1

>>> s.count('e', -1)

1

str.decode()

解码,把指定编码格式的字符串,改变成内存中通用的Unicode格式

>>> s = 'abc'

>>> s.decode('utf-8')

u'abc'

>>> s

'abc'

str.encode()

编码,把内存中的Unicode格式改为指定的编码格式,方便存储。

>>> s = 'abc'

>>> s

'abc'

>>> s.encode('utf-8')

'abc'

str.endswith()

判断字符串后缀,即字符串末尾是不是指定字符串/字符。如果是返回True,如果不是返回False

>>> s

'abc'

>>> s.endswith('a')

False

>>> s.endswith('c')

True

>>> s.endswith('bc')

True

>>> s.endswith('a', 0, 1)

True

str.expandtabs()

把字符串中的制表符替换为空格然后返回新的字符串。默认制表符会被8个空格替换,可以指定几个空格替换

>>> s = 'a\tb'

>>> print s

a       b

>>> a = s.expandtabs()

>>> a

'a       b'

>>> a = s.expandtabs(4)

>>> a

'a   b'

str.find()

字符串中匹配子串,从前向后匹配返回第一次完全匹配的第一个字符的索引值(下标),如果没有匹配成功返回-1

>>> s = 'test,this is test'

>>> s.find('is')

7

>>> s.find('test',1)

13

>>> s.find('abc')

-1

str.format()

返回格式化字符串。

 

str.index()

返回指定字符在字符串中的位置,如果有多个只返回从前向后的第一个。和str.find()类似,但是如果查找没有那个字符串则会返回错误。

>>> s = 'this is test'

>>> s.index('i')

2

str.isalnum()

如果字符串中只包含字母和数字(或者只是数字/字母),返回True,否则返回False

>>> s.isal

s.isalnum( s.isalpha(

>>> s.isalnum()

False

>>> s = 'a'

>>> s.isalnum()

True

>>> s = '1'

>>> s.isalnum()

True

str.isalpha()

判断字符串是否只包含字母,如果是返回True,如果不是返回False

>>> s

'this is test'

>>> s.isalpha()

False

>>> s = 'ba'

>>> s.isalpha()

True

str.isdigit()

判断是否只包含数字,如果是返回True,如果不是返回False

>>> s

'ba'

>>> s.isdigit()

False

>>> s = '019'

>>> s.isdigit()

True

>>> s = '019 1'

>>> s.isdigit()

False

str.islower()

判断字符串中字母是否全部为小写字母,如果是返回True,如果不是返回False

>>> s = 'this'

>>> s.islower()

True

>>> s = 'This'

>>> s.islower()

False

>>> s = 'this is lower'

>>> s.islower()

True

>>> s = 'this is lower 123'

>>> s.islower()

True

str.isspace()

判断字符串是否全部是空白符(制表符/空格的组合),如果是返回True,如果不是返回False

>>> s = 'this is lower 123'

>>> s.isspace()

False

>>> s = ' this'

>>> s.isspace()

False

>>> s = '    '

>>> s.isspace()

True

>>> s = '\t'

>>> s.isspace()

True

str.istitle()

判断字符串是否满足标题要求(标题就是字符中以空白符分割的每个单词的首字母大写,其他字母小写),如果满足则返回True,如果不满足返回False

>>> s = 'this is title'

>>> s.istitle()

False

>>> s = 'This is title'

>>> s.istitle()

False

>>> s = 'This Is Title'

>>> s.istitle()

True

>>> s = 'THIS IS TITLE'

>>> s.istitle()

False

str.isupper()

判断字符串中字母是否全为大写,如果是返回True,如果不是返回Flase

>>> s = 'THIS IS UPPER'

>>> s.isupper()

True

>>> s = 'This'

>>> s.isupper()

False

str.join()

用指定字符/字符串来连接一个可迭代对象并以字符串形式返回结果

>>> s = '+'

>>> s.join(map(str,range(10)))

'0+1+2+3+4+5+6+7+8+9'

str.ljust()

指定字符串宽度(字符串占几个字符宽度),默认不足用空格填充。源字符串在新的字符串的最左侧(left)

>>> s ='test'

>>> s.ljust(10)

'test      '

>>> s.ljust(10,'-')

'test------'

str.lower()

把字符串中所有字符全部置换成小写字母,然后返回新的字符串。

 

str.lstrip()

删除字符最前面的指定字符,默认删除换行字符或空白字符。

>>> s = 'abc'

>>> s.lstrip()

'abc'

>>> s.lstrip('a')

'bc'

>>> s = 'abc\n'

>>> s.lstrip()

'abc\n'

>>> s = '\nabc'

>>> s.lstrip()

'abc'

str.mro()

 

 

str.partition()

用指定字符串分割源字符串(从前向后依次匹配)。以元组形式返回,返回的元组包含三个元素,分割字符串之前的字符组成的字符串(如果没有则位空字符串),分割字符串,分割字符串之后的字符串,如果字符串中没有这个用来分割的字符串,则返回的元组中后两个元素位空字符串

>>> s = 'this is test'

>>> s.partition('is')

('th', 'is', ' is test')

>>> s.partition(' ')

('this', ' ', 'is test')

>>> s.partition('this')

('', 'this', ' is test')

>>> s.partition('test')

('this is ', 'test', '')

>>> s.partition('are')

('this is test', '', '')

str.replace()

用新的子串替换源字符串中老的子串,并返回新的字符串(源字符串不变),可以指定替换几次,默认全部替换(匹配顺序是自前向后依次匹配)

>>> s = 'this is test'

>>> s.replace('is','ab')

'thab ab test'

>>> s.replace('is', 'are', 1)

'thare is test'

str.rfind()

查找子串,如果包含子串,则返回最后一个子串的第一个匹配字符的索引值。如果没有这个子串则返回-1

>>> s = 'this is test'

>>> s.rfind('i')

5

>>> s.rfind('s')

10

>>> s.rfind('t')

11

>>> s.rfind('are')

-1

str.rindex()

str.rfind()类似,但是当没有子串时返回错误。

>>> s = 'this is test'

>>> s.rindex('is')

5

str.rjust()

str.ljust()类似,指定子串长度并把源字符串放在新字符串最右端,如果源字符串长度不够,默认使用空格填充,(right)

>>> s = 'test'

>>> s.rjust(10)

'      test'

>>> s.rjust(10,'-')

'------test'

str.rpartition()

str.partition()类似。这个功能时从右向左匹配用第一次匹配到的字符分割。如果没有则返回的元组中前两个元素为空,第三个元素为源字符串。

>>> s = 'this is test'

>>> s.rpartition('is')

('this ', 'is', ' test')

>>> s.rpartition('t')

('this is tes', 't', '')

>>> s.rpartition('are')

('', '', 'this is test')

str.rsplit()

在不指定最大分割次数时作用和str.split()相同。匹配分割字符时顺序是从右向左(right),所以指定最大分割次数后的分割效果和str.split()会有差别。

>>> s = 'this is test'

>>> s.rsplit('is')

['th', ' ', ' test']

>>> s.split('is')

['th', ' ', ' test']

>>> s.rsplit('is',1)

['this ', ' test']

>>> s.split('is',1)

['th', ' is test']

str.rstrip()

str.lstrip()的去除字符位置相反,删除字符串末尾指定字符,默认去除字符串末尾的空白字符。并返回新的字符串,源字符串不变

>>> s = 'test '

>>> s.rstrip()

'test'

>>> s.rstrip('t ')

'tes'

str.split()

分割字符串转换为列表,默认用空格和制表符分割,默认从前向后匹配。可以指定分割的最大分割次数。

>>> s = 'this is test'

>>> s.split()

['this', 'is', 'test']

>>> s.split('is')

['th', ' ', ' test']

>>> s.split(' ', 1)

['this', 'is test']

str.splitlines()

把字符串分割成不同行,用换行符分割。每行都是列表中的一个元素。并返回这个列表。返回值中每一行末尾的换行符都已经删除。

>>> s = 'this is test'

>>> s.splitlines()

['this is test']

>>> s = 'this is test.\nYes'

>>> s.splitlines()

['this is test.', 'Yes']

str.startswith()

字符串以XXX开始,如果匹配则返回True,如果不匹配返回False

>>> s = 'this is test'

>>> s.startswith('this')

True

str.strip()

脱去字符串的开始和结尾,默认去除“\n或者\t或者空格”可以看作是str.lstrip()str.rstrip()的结合体。

 

str.swapcase()

把字符串中所有字母的大小写互换,即字符串中的大写字母转换为小写,小写字母转换为大写,然后返回这个新的字符串,源字符串不变。

>>> s = 'This is Test'

>>> s.swapcase()

'tHIS IS tEST'

>>> s = 'This is 123'

>>> s.swapcase()

'tHIS IS 123'

str.title()

把字符串的格式转换为标题格式(标题就是字符中以空白符分割的每个单词的首字母大写,其他字母小写)

>>> s = 'this is title 123'

>>> s.title()

'This Is Title 123'

str.translate()

根据转换表(maketrans函数定义转换表Python2中这个函数在string模块内,Python3可以直接用str.maketrans()创建转换表)。用转换表中需要转换的字符组替换为目标字符,如果有多种源字符需要替换为对应的目标字符,则需要把源字符按顺序排列组成一个字符串和目标字符组成的字符串一一对应。

>>> import string

>>> intab = 'is'

>>> outtab = '23'

>>> deltab = 'e'

>>> trantab = string.maketrans(intab, outtab)

>>> s = 'This Is Test'

>>> s.translate(trantab)

'Th23 I3 Te3t'

>>> s.translate(trantab, deltab)

'Th23 I3 T3t'

str.upper()

把字符串中字母转换为大写字母并返回这个新的字符串。源字符串不变。

>>> s = 'this is Test'

>>> s.upper()

'THIS IS TEST'

str.zfill()

返回指定长度的字符串,如果长度不够在源字符串左侧用0填充。

>>> s = 'test'

>>> s.zfill(10)

'000000test'

>>> s.zfill(1)

'test'

 

1.2、list,列表常用方法

以下举例是python2.7测试:

函数名称

作用

举例

list.append(x)

将元素X添加至列表list尾部

>>> l1 = ['a','b']

>>> y = 'abc'

>>> l1.append(y)

>>> l1

['a', 'b', 'abc']

list.count(x)

返回指定元素x在列表中出现次数

>> li = map(str,range(5))

>>> li

['0', '1', '2', '3', '4']

>>> li.index('1')

1

list.extend(L)

L中的所有元素添加至列表list尾部

>>> l1 = ['a','b']

>>> y = 'abc'

>>> l1.extend(y)

>>> l1

['a', 'b', 'abc', 'a', 'b', 'c']

list.index(x)

返回元素从前向后的第一个元素x的下标。如果没有x则抛出异常

>>> li = map(str,range(5))

>>> li

['0', '1', '2', '3', '4']

>>> li.index('3')

3

list.insert(index,x)

在位置index处添加元素xindex之后的每个元素后移一个。

>>> l1 = [1, 2, 3]

>>> y = 'abc'

>>> l1.insert(0,y)

>>> l1

['abc', 1, 2, 3]

list.mro()

 

 

list.pop([index])

返回下标为index的元素。并删除下标为index的元素(默认index-1

>>> l = list('abcd')

>>> y = l.pop(-1)

>>> y

'd'

>>> l

['a', 'b', 'c']

list.remove(x)

删除在列表list中首次出现的元素x,之后的元素向前移一个位置。

>>> l = list('abc')

>>> l.remove('a')

>>> l

['b', 'c']

list.reverse()

对列表list进行逆序

>>> l = [1,2,3]

>>> l.reverse()

>>> l

[3, 2, 1]

list.sort(cmp=None, key=None, reverse=False)

list进行排序。用key指定排序依据,用reverse决定升序(False)还是降序(True)

>>> L = ['b','a','c','1']

>>> L.sort()

>>> L

['1', 'a', 'b', 'c']

>>> L.sort(reverse=True)

>>> L

['c', 'b', 'a', '1']

 

1.3、tuple,元组常用方法

以下举例是python2.7测试:

函数名称

作用

a.count()

返回指定元素x在元组a中出现次数

a.index()

返回元组中指定元素的索引值(下标),如果没有这个元素则报错

 

二、方法详解

2.1、string,字符串类和内置方法详解

 

2.1、list,列表类和内置方法详解

 

2.1、tuple,元组类和内置方法详解

posted @ 2018-08-16 21:13  mehome  阅读(686)  评论(0编辑  收藏  举报