一、常用方法集合

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处添加元素x,index之后的每个元素后移一个。

>>> 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,元组类和内置方法详解

序列内置方法详解(string/list/tuple)的更多相关文章

  1. Python内置方法详解

    1. 字符串内置方法详解 为何要有字符串?相对于元组.列表等,对于唯一类型的定义,字符串具有最简单的形式. 字符串往往以变量接收,变量名. 可以查看所有的字符串的内置方法,如: 1> count ...

  2. for循环与内置方法详解

    ''' for循环与内置方法详解 ''' # 循环:重复(按照某种规律的)做一件事情 # lt = [1, 2, 3, 4] # # ind = 0 # # while True: # print(l ...

  3. Python_序列对象内置方法详解_String

    目录 目录 前言 软件环境 序列类型 序列的操作方法 索引调用 切片运算符 扩展切片运算符 序列元素的反转 连接操作符 重复运算符 成员关系符 序列内置方法 len 获取序列对象的长度 zip 混合两 ...

  4. 数字内置方法详解(int/long/float/complex)

    一.常用方法 1.1.int 以下是Python2.7的int内置函数: 序号 函数名 作用 举例 1 int.bit_length() 二进制存储这个整数至少需要多少bit(位). >> ...

  5. Python_List对象内置方法详解

    目录 目录 前言 软件环境 列表List 修改列表的元素 插入列表元素 extend 将序列中的元素迭代的附加到list中 insert 在指定的索引号中插入一个元素 删除列表元素 del 删除Lis ...

  6. python内置常用内置方法详解

    # print(locals()) # print(globals()) def func(): x = 1 y = 1 print(locals()) # 函数内部的变量 print(globals ...

  7. python基础-内置函数详解

    一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...

  8. python3 内置函数详解

    内置函数详解 abs(x) 返回数字的绝对值,参数可以是整数或浮点数,如果参数是复数,则返回其大小. # 如果参数是复数,则返回其大小. >>> abs(-25) 25 >&g ...

  9. JavaWeb学习----JSP内置对象详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

随机推荐

  1. CC14:集合栈

    题目 请实现一种数据结构SetOfStacks,由多个栈组成,其中每个栈的大小为size,当前一个栈填满时,新建一个栈.该数据结构应支持与普通栈相同的push和pop操作. 给定一个操作序列int[] ...

  2. Codeforces Round #566 (Div. 2) A. Filling Shapes

    链接: https://codeforces.com/contest/1182/problem/A 题意: You have a given integer n. Find the number of ...

  3. 关于codeblock 为什么不能调试

    最近codeblock不能调试了,一开始还以为把断点放在了函数里面,所以不行. 代码短,就自己看了, 有时候实在不行,真的要调试,那怎么办?其实很多时候是你的文件名的问题. 1.project的路径必 ...

  4. spring data jpa 简单使用

    通过解析方法名创建查询 通过前面的例子,读者基本上对解析方法名创建查询的方式有了一个大致的了解,这也是 Spring Data JPA 吸引开发者的一个很重要的因素.该功能其实并非 Spring Da ...

  5. es6+字符串string的新增方法函数

    String.includes("xxx")   返回true/false     [es5的字符串查找方法:String.indexOf() ] String.startsWit ...

  6. Ionic 2 中的创建一个闪视卡片组件

    闪视卡片是记忆信息的重要工具,它的使用可以追溯到19世纪.我们将要创建一个很酷的短暂动画来实现它.看起来像是这个样子的: 闪视卡片示例 Ionic 2 实例开发 新增章节将为你介绍如何在Ionic 2 ...

  7. GIT新手入门学习教程

    廖雪峰的GIT教程 链接地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  8. GetRelativePath获取相对路径

    public static string GetRelativePath(string baseDirPath, string subFullPath) { // ForceBasePath to a ...

  9. 一个SQL server的事务存储

    -- ============================================= -- Author: Evan -- Create date: 2018年6月14日 16点27分 - ...

  10. centos上手动安装最新版本ELK

    软件包下载地址:https://www.elastic.co/downloads/elasticsearch 1,安装es #tar zxvf elasticsearch-2.3.4.tar.gz # ...