Python的内置字符串方法.docx
《Python的内置字符串方法.docx》由会员分享,可在线阅读,更多相关《Python的内置字符串方法.docx(11页珍藏版)》请在冰豆网上搜索。
Python的内置字符串方法
Python的内置字符串方法(收藏专用)
字符串处理是非常常用的技能,但Python内置字符串方法太多,常常遗忘,为了便于快速参考,特地依据Python3.5.1给每个内置方法写了示例并进行了归类,便于大家索引。
PS:
可以点击概览内的绿色标题进入相应分类或者通过右侧边栏文章目录快速索引相应方法。
概览
字符串大小写转换
lstr.capitalize()
lstr.lower()
lstr.casefold()
lstr.swapcase()
lstr.title()
lstr.upper()
字符串格式输出
lstr.center(width[,fillchar])
lstr.ljust(width[,fillchar]);str.rjust(width[,fillchar])
lstr.zfill(width)
lstr.expandtabs(tabsize=8)
lstr.format(^args,^^kwargs)
lstr.format_map(mapping)
字符串搜索定位与替换
lstr.count(sub[,start[,end]])
lstr.find(sub[,start[,end]]);str.rfind(sub[,start[,end]])
lstr.index(sub[,start[,end]]);str.rindex(sub[,start[,end]])
lstr.replace(old,new[,count])
lstr.lstrip([chars]);str.rstrip([chars]);str.strip([chars])
lstaticstr.maketrans(x[,y[,z]]);str.translate(table)
字符串的联合与分割
lstr.join(iterable)
lstr.partition(sep);str.rpartition(sep)
lstr.split(sep=None,maxsplit=-1);str.rsplit(sep=None,maxsplit=-1)
lstr.splitlines([keepends])
字符串条件判断
lstr.endswith(suffix[,start[,end]]);str.startswith(prefix[,start[,end]])
lstr.isalnum()
lstr.isalpha()
lstr.isdecimal();str.isdigit();str.isnumeric()
lstr.isidentifier()
lstr.islower()
lstr.isprintable()
lstr.isspace()
lstr.istitle()
lstr.isupper()
字符串编码
lstr.encode(encoding=”utf-8″,errors=”strict”)
大小写转换
str.capitalize()
将首字母转换成大写,需要注意的是如果首字没有大写形式,则返回原字符串。
1
2
3
4
5
6
7
8
9
10
11
'adidog'.capitalize()
#'Adidog'
'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'
str.casefold()
将字符串转换成小写,Unicode编码中凡是有对应的小写形式的,都会转换。
1
2
3
4
5
'DOBI'.casefold()
#'dobi'
'?
'.casefold()#德语中小写字母?
等同于小写字母ss,其大写为SS
#'ss'
str.swapcase()
对字符串字母的大小写进行反转。
1
2
'徐Dobia123?
'.swapcase()
#:
'徐dOBIA123SS'这里的?
被转成SS是一种大写
但需要注意的是 s.swapcase().swapcase()==s 不一定为真:
1
2
3
4
5
6
7
8
9
10
11
u'xb5'
#'μ'
u'xb5'.swapcase()
#'Μ'
u'xb5'.swapcase().swapcase()
#'μ'
hex(ord(u'xb5'.swapcase().swapcase()))
Out[154]:
'0x3bc'
这里 'Μ'(是mu不是M)的小写正好与 'μ' 的写法一致。
str.title()
将字符串中每个“单词”首字母大写。
其判断“单词”的依据则是基于空格和标点,所以应对英文撇好所有格或一些英文大写的简写时,会出错。
1
2
3
4
5
6
7
8
9
'Helloworld'.title()
#'HelloWorld'
'中文abcdef12gh'.title()
#'中文AbcDef12Gh'
#但这个方法并不完美:
"they'rebill'sfriendsfromtheUK".title()
#"They'ReBill'SFriendsFromTheUk"
str.upper()
将字符串所有字母变为大写,会自动忽略不可转成大写的字符。
1
2
'中文abcdef12gh'.upper()
#'中文ABCDEF12GH'
需要注意的是 s.upper().isupper() 不一定为 True。
字符串格式输出
str.center(width[,fillchar])
将字符串按照给定的宽度居中显示,可以给定特定的字符填充多余的长度,如果指定的长度小于字符串长度,则返回原字符串。
1
2
3
4
5
'12345'.center(10,'*')
#'**12345***'
'12345'.center(10)
#'12345'
str.ljust(width[,fillchar]);str.rjust(width[,fillchar])
返回指定长度的字符串,字符串内容居左(右)如果长度小于字符串长度,则返回原始字符串,默认填充为ASCII空格,可指定填充的字符串。
1
2
3
4
5
6
7
8
9
10
11
'dobi'.ljust(10)
#'dobi'
'dobi'.ljust(10,'~')
#'dobi~~~~~~'
'dobi'.ljust(3,'~')
#'dobi'
'dobi'.ljust(3)
#'dobi'
str.zfill(width)
用‘0’填充字符串,并返回指定宽度的字符串。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"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)
用指定的空格替代横向制表符,使得相邻字符串之间的间距保持在指定的空格数以内。
1
2
3
4
5
6
7
8
9
tab='1t23t456t7890t1112131415t161718192021'
tab.expandtabs()
#'12345618192021'
#'567812345678'注意空格的计数与上面输出位置的关系
tab.expandtabs(4)
#'12345618192021'
#'34123412341234'
str.format(^args,^^kwargs)
格式化字符串的语法比较繁多,官方文档已经有比较详细的examples,这里就不写例子了,想了解的童鞋可以直接戳这里 Formatexamples.
str.format_map(mapping)
类似 str.format(*args,**kwargs) ,不同的是 mapping 是一个字典对象。
1
2
3
4
People={'name':
'john','age':
56}
'Mynameis{name},iam{age}old'.format_map(People)
#'Mynameisjohn,iam56old'
字符串搜索定位与替换
str.count(sub[,start[,end]])
1
2
3
4
5
6
7
8
9
10
text='outerprotectivecovering'
text.count('e')
#4
text.count('e',5,11)
#1
text.count('e',5,10)
#0
str.find(sub[,start[,end]]);str.rfind(sub[,start[,end]])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
text='outerprotectivecovering'
text.find('er')
#3
text.find('to')
#-1
text.find('er',3)
Out[121]:
3
text.find('er',4)
Out[122]:
20
text.find('er',4,21)
Out[123]:
-1
text.find('er',4,22)
Out[124]:
20
text.rfind('er')
Out[125]:
20
text.rfind('er',20)
Out[126]:
20
text.rfind('er',20,21)
Out[129]:
-1
str.index(sub[,start[,end]]);str.rindex(sub[,start[,end]])
继续阅读