当前位置:文档之家› Python 的内置字符串方法

Python 的内置字符串方法

字符串处理是非常常用的技能,但Python 内置字符串方法太多,常常遗忘,为了便于快速参考,特地依据Python 3.5.1 给每个内置方法写了示例并进行了归类,便于大家索引。

PS: 可以点击概览内的绿色标题进入相应分类或者通过右侧边栏文章目录快速索引相应方法。

概览字符串大小写转换∙str.capitalize()∙str.lower()∙str.casefold()∙str.swapcase()∙str.title()∙str.upper()字符串格式输出∙str.center(width[, fillchar])∙str.ljust(width[, fillchar]); str.rjust(width[, fillchar])∙str.zfill(width)∙str.expandtabs(tabsize=8)∙str.format(^args, ^^kwargs)∙str.format_map(mapping)字符串搜索定位与替换∙str.count(sub[, start[, end]])∙str.find(sub[, start[, end]]); str.rfind(sub[, start[, end]])∙str.index(sub[, start[, end]]); str.rindex(sub[, start[, end]])∙str.replace(old, new[, count])∙str.lstrip([chars]); str.rstrip([chars]); str.strip([chars])∙static str.maketrans(x[, y[, z]]); str.translate(table)字符串的联合与分割∙str.join(iterable)∙str.partition(sep); str.rpartition(sep)∙str.split(sep=None, maxsplit=-1); str.rsplit(sep=None, maxsplit=-1) ∙str.splitlines([keepends])字符串条件判断∙str.endswith(suffix[, start[, end]]); str.startswith(prefix[, start[, end]]) ∙str.isalnum()∙str.isalpha()∙str.isdecimal(); str.isdigit(); str.isnumeric()∙str.isidentifier()∙str.islower()∙str.isprintable()∙str.isspace()∙str.istitle()∙str.isupper()字符串编码∙str.encode(encoding=”utf-8″, errors=”strict”)1 2 3 4 5 6 7 8 91011 'adidog'.capitalize() # 'Adi dog''abcd徐'.capitalize() # 'Abcd徐''徐abcd'.capitalize() # '徐abcd''ß'.capitalize()# 'SS'str.lower()将字符串转换成小写,其仅对ASCII编码的字母有效。

1 2 3 4 5 6 7 8 'DOBI'.lower()# 'dobi''ß'.lower() # 'ß' 为德语小写字母,其有另一种小写'ss',lower 方法无法转换# 'ß''徐ABCD'.lower()# '徐abcd'1 2 3 4 5 'DOBI'.casefold()# 'dobi''ß'.casefold() #德语中小写字母ß等同于小写字母ss,其大写为SS# 'ss'1 2 '徐Dobi a123 ß'.swapcase()#: '徐dOBI A123 SS' 这里的ß被转成SS 是一种大写但需要注意的是s.swapcase().swapcase() == s不一定为真:1 2 3 4 5 6 7 8 91011 u'xb5'# 'µ'u'xb5'.swapcase()# 'Μ'u'xb5'.swapcase().swapcase()# 'μ'hex(ord(u'xb5'.swapcase().swapcase())) Out[154]:'0x3bc'1 2 3 4 5 6 7 8 9 'Hello world'.title()# 'Hello World''中文abcdef 12gh'.title()# '中文AbcDef 12Gh'# 但这个方法并不完美:"they're bill's friends from the UK".title() # "They'ReBill'S Friends From The Uk"str.upper()将字符串所有字母变为大写,会自动忽略不可转成大写的字符。

1 2 '中文abcdef 12gh'.upper()# '中文ABC DEF 12GH'12 3 4 5 '12345'.center(10,'*')# '**12345***''12345'.center(10)# ' 12345 '1 2 3 4 'dobi'.ljust(10)# 'dobi ''dobi'.ljust(10,'~')5 6 7 8 91011 # 'dobi~~~~~~''dobi'.ljust(3,'~') # 'dobi''dobi'.ljust(3) # 'dobi'1 2 3 4 5 6 7 8 910111213141516171819 "42".zfill(5)# '00042'"-42".zfill(5)# '-0042''dd'.zfill(5)# '000dd''--'.zfill(5)# '-000-'' '.zfill(5)# '0000 '''.zfill(5)# '00000''dddddddd'.zfill(5) # 'dddddddd'str.expandtabs(tabsize=8)用指定的空格替代横向制表符,使得相邻字符串之间的间距保持在指定的空格数以内。

123456789 tab='1t23t456t7890t1112131415t161718192021' tab.expandtabs() # '1 23 456 7890 1112131415 161718192021' # '123456781234567812345678123456781234567812345678' 注意空格的计数与上面输出位置的关系 tab.expandtabs(4) # '1 23 456 7890 1112131415 161718192021'# '12341234123412341234123412341234'1 2 3 4 People={'name':'john','age':56}'My name is {name},i am {age} old'.format_map(People)# 'My name is john,i am 56 old'1 2 3 4 5 6 7 8 9 10 text='outer protective covering'text.count('e')# 4text.count('e',5,11)# 1text.count('e',5,10)# 01 2 3 4 5 6 7 8 9101112131415161718192021222324252627 text='outer protective covering'text.find('er')# 3text.find('to')# -1text.find('er',3)Out[121]:3text.find('er',4)Out[122]:20text.find('er',4,21)Out[123]:-1text.find('er',4,22)Out[124]:20text.rfind('er')Out[125]:20text.rfind('er',20)Out[126]:20text.rfind('er',20,21)28 Out[129]:-11 2 3 4 5 6 7 89 10 11 12 13 14 'dog wow wowjiao'.replace('wow','wang')# 'dog wangwangjiao''dog wow wowjiao'.replace('wow','wang',1)# 'dog wang wow jiao''dog wow wowjiao'.replace('wow','wang',0)# 'dog wow wowjiao''dog wow wowjiao'.replace('wow','wang',2)# 'dog wangwangjiao''dog wow wowjiao'.replace('wow','wang',3)# 'dog wangwangjiao'1 2 3 4 5 6 7 ' dobi'.lstrip()# 'dobi'''.lstrip('dbk')# ''' dobi '.rstrip()# ' dobi'8 910111213141516 ''.rstrip('acn') # 'db.kun.ac.'' dobi '.strip()# 'dobi'''.strip('db.c') # ''''.strip('cbd.un') # 'kun.a'1 2 3 4 5 6 7 8 9101112131415 a='dobi'ord('o')# 111ord('a')# 97hex(ord('狗'))# '0x72d7'b={'d':'dobi',111:' is ','b':97,'i':'u72d7u72d7'} table=str.maketrans(b)a.translate(table)# 'dobi is a狗狗'如果maktrans有两个参数,则两个参数形成映射,且两个字符串必须是长度相等;如果有第三个参数,则第三个参数也必须是字符串,该字符串将自动映射到None:1 2 3 4 5 6 7 8 91011 a='dobi is a dog'table=str.maketrans('dobi','alph')a.translate(table)# 'alphhs a alg'table=str.maketrans('dobi','alph','o')a.translate(table)# 'aphhs a ag'1 2 3 4 5 6 7 8 910111213141516 '-'.join(['2012','3','12'])# '2012-3-12''-'.join([2012,3,12])# TypeError: sequence item 0: expected str instance, int found'-'.join(['2012','3',b'12']) #bytes 为非字符串# TypeError: sequence item 2: expected str instance, bytes found'-'.join(['2012'])# '2012''-'.join([])# '''-'.join([None])17181920212223242526 # TypeError: sequence item 0: expected str instance, NoneType found'-'.join([''])# ''','.join({'dobi':'dog','polly':'bird'})# 'dobi,polly'','.join({'dobi':'dog','polly':'bird'}.values())# 'dog,bird'1 2 3 4 5 6 7 8 910111213141516171819202122232425 'dog wow wowjiao'.partition('wow') # ('dog ', 'wow', ' wow jiao')'dog wow wowjiao'.partition('dog') # ('', 'dog', ' wow wowjiao')'dog wow wowjiao'.partition('jiao') # ('dog wow wow ', 'jiao', '')'dog wow wowjiao'.partition('ww') # ('dog wow wowjiao', '', '')'dog wow wowjiao'.rpartition('wow') Out[131]:('dog wow ','wow',' jiao')'dog wow wowjiao'.rpartition('dog') Out[132]:('','dog',' wow wowjiao')'dog wow wowjiao'.rpartition('jiao') Out[133]:('dog wow wow ','jiao','')'dog wow wowjiao'.rpartition('ww') Out[135]:('','','dog wow wowjiao')1 2 3 4 5 6 7 8 91011121314151617181920212223242526 '1,2,3'.split(','),'1, 2, 3'.rsplit()# (['1', '2', '3'], ['1,', '2,', '3'])'1,2,3'.split(',',maxsplit=1), '1,2,3'.rsplit(',',maxsplit=1) # (['1', '2,3'], ['1,2', '3'])'1 2 3'.split(),'1 2 3'.rsplit()# (['1', '2', '3'], ['1', '2', '3'])'1 2 3'.split(maxsplit=1),'1 2 3'.rsplit(maxsplit=1)# (['1', '2 3'], ['1 2', '3'])' 1 2 3 '.split()# ['1', '2', '3']'1,2,,3,'.split(','),'1,2,,3,'.rsplit(',')# (['1', '2', '', '3', ''], ['1', '2', '', '3', ''])''.split()# []''.split('a')# ['']'bcd'.split('a')# ['bcd']'bcd'.split(None)# ['bcd']str.splitlines([keepends])字符串以行界符为分隔符拆分为列表;当keepends为True,拆分后保留行界符,能被识别的行界符见官方文档。

相关主题