《Python基础教程》读书笔记03-字符串
字符串都是不可变的。
string 模块
字符串方法完全源于 string 模块,这个模块还包括一些不能作为字符串方法使用的常量和函数。如:maketrans
函数
一些有用的字符串常量(import string
):
string.digits
:0-9
,包含数字 0~9 的字符串string.letters
:a-zA-Z
,包含所有字母的字符串(与地区有关,Python3.0 需要使用string.acill_letters
)string.lowercase
:a-z
,包含所有小写字母的字符串string.uppercase
:A-Z
,包含所有大写字母的字符串string.punctuation
:!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
,包含所有标点的字符串string.printable
:0-9a-zA-Z!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c
,包含所有可打印字符的字符串
查找 find,index,count,startswiith,endswiith
find
方法可以子啊一个字符串中查找子串,返回子串所在位置的最左端索引,如果没有返回-1
。
可以设置起始和终止值指定的范围(第二个和第三个参数),包含前面不包含后边。>>> x = '12345678901234' >>> x.find('345') 2 >>> x.find('23', 10) 11 >>> x.find('34', 2, 10) 2
rfind(sub[, start[, end]])
方法,返回子串 sub 被找到的位置的最后一个索引,不存在返回 -1,可以设置起始和终止值指定的范围。index(sub[, start[, end]])
方法,返回子串 sub 的第一个索引,如果不存在引发异常,可以设置起始和终止值指定的范围.rindex(sub[, start[, end]])
方法,返回子串 sub 被找到的位置的最后一个索引,如果不存在引发异常,可以设置起始和终止值指定的范围。count(sub[, start[, end]])
方法,返回子串 sub 出现的次数,可以设置起始和终止值指定的范围。startswiith(prefix[, start[, end]])
方法,检查 string 是否以 prefix 开始,返回 True、False,可以设置起始和终止值指定的范围。endswiith(suffix[, start[, end]])
方法,检查 string 是否以 suffix 结尾,返回 True、False,可以设置起始和终止值指定的范围。
连接 join,分割 split,splitlines
join
方法是 split 方法的逆方法,用来连接序列中的元素,被连接的序列元素必须都是字符串,>>> seq = [1, 2, 3, 4, 5] >>> sep = '+' >>> sep.join(seq) # 不能连接数字 Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> sep.join(seq) TypeError: sequence item 0: expected string, int found >>> seq = ['1','2','3','4','5',] >>> sep.join(seq) '1+2+3+4+5' >>> dirs = '', 'usr', 'bin', 'enc' >>> '/'.join(dirs) '/usr/bin/enc'
split
方法用来将字符串分割成序列,第二个参数可以指定最大分割数。>>> '1+2+3+4+5'.split('+') ['1', '2', '3', '4', '5'] >>> 'Using the default'.split() # 默认把所有空格作为分隔符(空格、制表、换行符等) ['Using', 'the', 'default'] >>> '1\n2'.split() ['1', '2'] >>> 'Using the default'.split(' ', 1) # 指定最大分割数 ['Using', 'the default']
rsplit
方法,同 split,但指定了最大分割数时,从右向左计数。>>> 'Using the default'.rsplit(' ', 1) ['Using the', 'default']
splitlinex([keepends])
方法。返回 string 中所有行的列表,可选择是否包含换行符,会对 keepends 参数进行运算>>> x = '''123 456 zbc xyz ''' >>> x.splitlines() ['123', '456', 'zbc', 'xyz'] >>> x.splitlines(1) ['123\n', '456\n', 'zbc\n', 'xyz\n'] >>> x.splitlines(0) ['123', '456', 'zbc', 'xyz'] >>> x.splitlines(True) ['123\n', '456\n', 'zbc\n', 'xyz\n'] >>> x.splitlines(False) ['123', '456', 'zbc', 'xyz']
字母大小写 lower,upper,capitalize,swapcase,title
lower
,返回字符串的小写字母版islower
,检查字符串中所有的字符是否都是小写upper
,返回字符串的大写字母版isupper
,检查字符串中所有的字符是否都是大写capitalize
,返回首字母大写的字符串,只是第一个字符大写swapcase
,返回字符串所有字符交换大小写title
,所有单词都以大写字母开头,>>> x = 'what\'s that' >>> x.title() # 以此为标题,不自然 "What'S That" >>> import string # 使用string模块下的capwords函数 >>> string.capwords(x) "What's That"
istitle
,检查所有单词是否都以大写字母开头,且其他字符都是小写>>> x = "What'S That" >>> x.istitle() True >>> x = "WhaT'S That" >>> x.istitle() False
替换 replace,translate,expandtabs
replace(old, new[, max]
,返回某字符串的所有匹配项均被替换之后的字符串,可选择最多可替换 max 个translate(table[,deletechars])
,和 replace 方法类似,translate
只处理单个字符,有时候效率会高,使用前需要完成一张转换表,第二个参数(Python2),可选择删除 deletechars 所出现的字符# Python2 >>> from string import maketrans >>> table = maketrans('he', 'wc') >>> len(table) # 包含ASCII字符集中的256个字符 256 >>> table[97:123] # h和e,已被替换 'abcdcfgwijklmnopqrstuvwxyz' >>> 'hello'.translate(table) 'wcllo' >>> 'hello'.translate(table,'o') # 第二个参数,可选择删除所出现的字符 'wcll' >>> 'helloc'.translate(table,'oc') # 看来是先删除,后替换 'wcll' >>> 'hello'.translate(table,'ol') 'wc' >>> table = maketrans('你', '好') # 可以为表中添加其他字符 >>> print '你'.translate(table) 好
# Python3 >>> from string import maketrans # 没有该函数 Traceback (most recent call last): File "<pyshell#32>", line 1, in <module> from string import maketrans ImportError: cannot import name 'maketrans' >>> table = ''.maketrans('he', 'ab') >>> table {104: 97, 101: 98} >>> 'wegerhtre'.translate(table) 'wbgbratrb' >>> 'wegerhtre'.translate(table, 'w') # 没有第二个参数 Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> 'wegerhtre'.translate(table, 'w') TypeError: translate() takes exactly one argument (2 given)
expandtabs(tabsize)
,其中的 tab 字符会用空格进行替换,可选择给定的 tabsize(默认为8)>>> x = '1\t1' >>> x.expandtabs(1) '1 1' >>> x.expandtabs() '1 1'
去除字符串两端 strip
strip([chars])
,去除字符串两端的 chars(默认空格,空白符)lstrip([chars]),
去除字符串前端的 chars(默认空格,空白符)rstrip([chars]),
去除字符串后端的 chars(默认空格,空白符)
>>> x = '1 2 1'
>>> x.strip('1')
' 2 '
填充字符串两端 center,ljust,rjust,zfill
center(width[, fillchar])
,返回一个长度为 max(len(string), width),其中 String 的副本居中的字符串,两侧使用 fillchar(默认空白符,单个字符)填充>>> x = '123' >>> x.center(10) ' 123 ' >>> x.center(10,'-') '---123----'
ljust(width[, fillchar])
,返回一个长度为 max(len(string), width),其中 String 的副本左对齐的字符串,两侧使用 fillchar(默认空白符)填充rjust(width[, fillchar])
,返回一个长度为 max(len(string), width),其中 String 的副本右对齐的字符串,两侧使用 fillchar(默认空白符)填充>>> x = 'a' >>> x.ljust(10,'b') 'abbbbbbbbb' >>> x.rjust(10,'b') 'bbbbbbbbba'
zfill(width)
,在 string 的左侧以 0 填充 width 个字符>>> x.zfill(10) '0000000123'
编码解码 decode,encode
decode([encodeing[, errors]])
,返回使用给定编码方式的字符串的解码版本,由 errors 指定错误方式('strict'、'ignore' 或 'replace')encode([encodeing[, errors]])
,返回使用给定编码方式的字符串的编码版本,由 errors 指定错误方式('strict'、'ignore' 或 'replace')
检查字符串组成 isalnum,isalpha,isdigit,isspace
isalnum
,检查字符串是否以字母或数字组成isalpha
,检查字符串是否由字母组成isdigit
,检查字符串是否由数字组成isspace
,检查字符串是否由空格(空白字符)组成
搜索子字符串 partition,rpartition
partition(sep)
,在字符串中搜索 sep 并返回 (head, sep, tail)rpartition(sep)
,同 partition,但从右边找
>>> x = 'abcdefgabcedefg'
>>> x.partition('de')
('abc', 'de', 'fgabcedefg')
>>> x.rpartition('de')
('abcdefgabce', 'de', 'fg')
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 bin07280@qq.com
文章标题:《Python基础教程》读书笔记03-字符串
文章字数:2.2k
本文作者:Bin
发布时间:2017-01-01, 16:51:21
最后更新:2019-08-06, 00:07:35
原始链接:http://coolview.github.io/2017/01/01/Python%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/%E3%80%8APython%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B%E3%80%8B%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B003-%E5%AD%97%E7%AC%A6%E4%B8%B2/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。