字符串的各种内置函数的使用:
capitalize() 将字符串的第一个字符变为大写,其余字符为小写
casefold() 将字符串的所有字符变为小写
center(width) 将字符串居中,并使用空格填充至长度为width的新字符串
count(sub[,start[,end]]) 返回sub在字符串中出现的次数,start和end表示寻找的返回 [start,end)
encode(encoding='utf-8',errors='strict') 以指定的编码格式对字符串进行编码,encoding -- 要使用的编码,如"UTF-8"。errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
decode(decoding='utf-8',errors='strict') 以指定的格式对字符串解码
endswith(sub[,start[,end]]) 检查字符串是否是以sub字符串结束,返回值为True和False,start和end参数表示范围,可选.
expandtabs(tabsize=8) 把字符串中的tab符号\t转换为空格,若不指定参数,默认的空格数是tabsize=8
find(sub[,start[,end]) 检测sub是否包含在字符串中,如果有则返回索引值,否则返回-1,start和end表示范围,可选
index(sub[,start[,end]) 与find方法相似,不过如果sub不在字符串中时会产生异常
isalnum() 判断字符串至少有一个字符并且所有字符都是字母或数字,返回值为True或False
isalpha() 字符串中至少有一个字符并且所有字符都是字母,返回值为True或False
isdecimal() 字符串中是否只包含十进制的数字,返回值为True或False
isdigit() 字符串中是否只包含数字
islower() 字符串中至少包含一个区分大小写的字符,并且这些字符都是小写
isnumeric() 字符串中是否只包含数字
isspace() 字符串中是否只包含空格
istitle() 判断字符串中是否每个单词的首字母为大写,其余都是小写
isupper() 字符串中至少包含一个区分大小写的字符,并且这些字符都是大写
join(sub) 将字符串作为分隔符,分隔sub中的所有字符
ljust(width) 返回一个左对齐的字符串,并用空格填充字符串至长度width
lower() 大写变小写
lstrip() 去掉左侧所有空格
partition(sub) 找到子字符串sub,把字符串分成一个三元组(pre_sub,sub,fol_sub),如果字符串不包含sub则返回(源字符串,'','')
replace(old,new[,count]) 把字符串中的old字符串替换成new字符串,如果没有count参数,则全部替换,否则替换count次
rfind(sub[,start[,end]]) 类似于find方法,只不过是从右边开始查找
rindex(sub[,start[,end]]) 类似于index方法,只不过是从右边开始查找
rjust(width) 返回一个右对齐的字符串,并用空格填充字符串至长度width
rpartition(sub) 类似于partition方法,只不过是从右查找
rstrip() 删除字符串末尾的字符
split(sep=None,maxsplit=-1) 不带参数默认以空格分割字符串,maxsplit为分割次数,返回一个列表
splitlines((keepends)) 按照'\n'分割,返回一个包含各行作为元素的列表
startswith(prefix[,start[,end]]) 检查字符串是否已prefix开头
strip(sub) 删除字符串两端的sub字符,不带参数时,默认删除字符串两端空格
swapcase() 反转字符串中的大小写
title() 返回标题化的字符串,每个单词的首字母为大写,其余字符为小写
translate(table) 根据table的规则(可以由str.maketrans('a','b')定制)转换字符串中的字符
upper() 将所有小写变为大写
zfill(width) 返回长度为width的字符串,原字符串右对齐,前面用0填充
函数使用举例:
>>> str1='zhao XIAOQIN'
>>> #capitalize()将字符串的第一个字符变为大写,其余字符为小写
>>> str1.capitalize()
'Zhao xiaoqin'
>>> #casefold()将字符串的所有字符变为小写
>>> str1.casefold()
'zhao xiaoqin'
>>> #center(width)将字符串居中,并使用空格填充至长度为width的新字符串
>>> str1.center(20)
' zhao XIAOQIN '
>>> #count(sub[,start[,end]])返回sub在字符串中出现的次数,start和end表示寻找的返回 [start,end)
>>> str1.count('I')
2
>>> str1.count('I',7)
1
>>> str1.count('I',7,10) #范围[7,10)
0
>>> #encode(encoding='utf-8',errors='strict') 以指定的编码格式对字符串进行编码
>>> str1='你好'
>>> str2=str1.encode('utf-8','strict')
>>> str2
b'\xe4\xbd\xa0\xe5\xa5\xbd'
>>> #decode(decoding='utf-8',errors='strict')以指定的格式对字符串解码
>>> str3=str2.decode('utf-8','ignore')
>>> str3
'你好'
>>> #endswith(sub[,start[,end]]) 检查字符串是否是以sub字符串结束,返回值为True和False,start和end参数表示范围,可选.
>>> str1='hi,I know you'
>>> str1.endswith('ou')
True
>>> str1.endswith('you',5)
True
>>> str1.endswith('you',5,8)
False
>>> #expandtabs(tabsize=8)把字符串中的tab符号\t转换为空格,若不指定参数,默认的空格数是tabsize=8
>>> str1='I\tknow\tyou'
>>> str1.expandtabs()
'I know you'
>>> str1.expandtabs(10)
'I know you'
>>> #find(sub[,start[,end]) 检测sub是否包含在字符串中,如果有则返回索引值,否则返回-1,start和end表示范围,可选
>>> str1='I know you'
>>> str1.find('ou')
8
>>> str1.find('ou',8)
8
>>> str1.find('ou',8,9)
-1
>>> #index(sub[,start[,end])与find方法相似,不过如果sub不在字符串中时会产生异常
>>> str1.index('ou',8,9)
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
str1.index('ou',8,9)
ValueError: substring not found
>>> #isalnum() 判断字符串至少有一个字符并且所有字符都是字母或数字,返回值为True或False
>>> str1='123zhao'
>>> str1.isalnum()
True
>>> #isalpha() 字符串中至少有一个字符并且所有字符都是字母,返回值为True或False
>>> str1.isalpha()
False
>>> #isdecimal()字符串中是否只包含十进制的数字,返回值为True或False
>>> str1='123'
>>> str1.isdecimal()
True
>>> #isdigit() 字符串中是否只包含数字
>>> str1.isdigit()
True
>>> #islower() 字符串中至少包含一个区分大小写的字符,并且这些字符都是小写
>>> str1='123asd'
>>> str1.islower()
True
>>> #isnumeric()字符串中是否只包含数字
>>> str1='12331'
>>> str1.isnumeric()
True
>>> str2=''
>>> str2.isnumeric()
False
>>> #isspace() 字符串中是否只包含空格
>>> str1=' '
>>> str1.isspace()
True
>>> str2=' jjj'
>>> str2.isspace()
False
>>> #istitle() 判断字符串中是否每个单词的首字母为大写,其余都是小写
>>> str2='I Love You'
>>> str2.istitle()
True
>>> str3='I love u'
>>> str3.istitle()
False
>>> #isupper() 字符串中至少包含一个区分大小写的字符,并且这些字符都是大写
>>>
>>> str1='asdAAA'
>>> str1.isupper()
False
>>> #join(sub),将字符串作为分隔符,分隔sub中的所有字符
>>> str1.join('你好啊')
'你asdAAA好asdAAA啊'
>>> #ljust(width) 返回一个左对齐的字符串,并用空格填充字符串至长度width
>>> str1.ljust(10)
'asdAAA '
>>> #lower()大写变小写
>>> str1.lower()
'asdaaa'
>>> #lstrip()去掉左侧所有空格
>>> str1=' eeedd ddd'
>>> str1.lstrip()
'eeedd ddd'
>>> #partition(sub) 找到子字符串sub,把字符串分成一个三元组(pre_sub,sub,fol_sub),如果字符串不包含sub则返回(源字符串,'','')
>>> str1=' eeedd ddd'
>>> str1.partition('ed')
(' ee', 'ed', 'd ddd')
>>> str1.partition('dee')
(' eeedd ddd', '', '')
>>> #replace(old,new[,count]) 把字符串中的old字符串替换成new字符串,如果没有count参数,则全部替换,否则替换count次
>>> str1='I know you'
>>> str1.replace('o','m')
'I knmw ymu'
>>> str1.replace('o','m',1)
'I knmw you'
>>> str1.replace('o','n',-1)
'I lnve ynu'
>>> #rfind(sub[,start[,end]])类似于find方法,只不过是从右边开始查找
>>> str1='I you you'
>>> str1.rfind('ou')
7
>>> str1.rfind('ou',3)
7
>>> str1.rfind('ou',3,5)
3
>>> #rindex(sub[,start[,end]])类似于index方法,只不过是从右边开始查找
>>> str1.rindex('ou',3,5)
3
>>> #rjust(width) 返回一个右对齐的字符串,并用空格填充字符串至长度width
>>> str1.rjust(10)
' I you you'
>>> #rpartition(sub),类似于partition方法,只不过是从右查找
>>> str1.rpartition('ou')
('I you y', 'ou', '')
>>> #rstrip() 删除字符串末尾的字符
>>> str1=' ssss ddd '
>>> str1.rstrip()
' ssss ddd'
>>> #split(sep=None,maxsplit=-1)不带参数默认以空格分割字符串,maxsplit为分割次数,返回一个列表
>>> str1='a aa aaa b'
>>> str1.split()
['a', 'aa', 'aaa', 'b']
>>> str1.split(' ',2) #指定分割次数2次,默认全部分割
['a', 'aa', 'aaa b']
>>> #splitlines((keepends)) 按照'\n'分割,返回一个包含各行作为元素的列表
>>> str1='''
我如果爱你
绝不做攀援的凌霄花
借你的高枝
炫耀自己
'''
>>> str1.splitlines()
['', '我如果爱你', '绝不做攀援的凌霄花', '借你的高枝', '炫耀自己']
>>> str1.splitlines()[2]
'绝不做攀援的凌霄花'
>>> str1.splitlines(0)
['', '我如果爱你', '绝不做攀援的凌霄花', '借你的高枝', '炫耀自己']
>>> str1.splitlines(1)
['\n', '我如果爱你\n', '绝不做攀援的凌霄花\n', '借你的高枝\n', '炫耀自己\n']
>>> str1.splitlines(183)
['\n', '我如果爱你\n', '绝不做攀援的凌霄花\n', '借你的高枝\n', '炫耀自己\n']
>>> str1.splitlines(True)
['\n', '我如果爱你\n', '绝不做攀援的凌霄花\n', '借你的高枝\n', '炫耀自己\n']
>>> str1.splitlines(False)
['', '我如果爱你', '绝不做攀援的凌霄花', '借你的高枝', '炫耀自己']
>>> #startswith(prefix[,start[,end]])检查字符串是否已prefix开头
>>> str1='我是flower'
>>> str1.startswith('我')
True
>>> str1.startswith('我',2,7)
False
>>> #strip(sub)删除字符串两端的sub字符,不带参数时,默认删除字符串两端空格
>>> str1='iiia 我 is hahah biii'
>>> str1.strip()
'iiia 我 is hahah biii'
>>> str1.strip('i')
'a 我 is hahah b'
>>> #swapcase()反转字符串中的大小写
>>> str1='I love U'
>>> str1.swapcase()
'i LOVE u'
>>> #title()返回标题化的字符串,每个单词的首字母为大写,其余字符为小写
>>> str2='ilove love you'
>>> str2.title()
'Ilove Love You'
>>> #translate(table) 根据table的规则(可以由str.maketrans('a','b')定制)转换字符串中的字符
>>> str1='i love you'
>>> str1.maketrans('o','m')
{111: 109} #111是‘o’的ASCII码,109是‘m’的ASCII码
>>> str1.translate(str1.maketrans('o','m'))
'i lmve ymu'
>>> #upper()将所有小写变为大写
>>> str1='i love U'
>>> str1.upper()
'I LOVE U'
>>> #zfill(width)返回长度为width的字符串,原字符串右对齐,前面用0填充
>>> str1.zfill(15)
'0000000i love U'