
1.5 str类的常用方法
案例24 转换字母的大小写
导语
str类公开了两个方法可以转换字母的大小写。lower方法返回新的字符串案例,并将传入的字符串中的大写字母转换为小写字母;upper方法的功能与lower方法正好相反,将小写字母转换为大写字母。
操作流程
步骤1:将字符串中的小写字母转换为大写字母。

第一次调用upper方法时,字符串案例中包含的全是大写字母,因此会把所有字符都转换为大写字母;第二次调用时,字符串案例中只有“klmn”是小写字母,所以在返回时只有这几个字符被转换为大写字母。
输出结果如下:

步骤2:以下代码将大写字母转换为小写字母。

与upper方法同理,字符串案例中大写字母会被转换为小写字母,而小写字母无须处理。输出结果如下:

步骤3:但是,对于中文与数字字符,lower和upper方法皆不起作用。

方法调用后,按原字符输出。

注意:swapcase方法也可以转换字母的大小写,此方法会将原字符串中的大写字母转换成小写字母,将小写字母转换成大写字母。例如:

得到的结果是“ABCdef”。
案例25 用“0”填充字符串
导语
zfill方法的作用是用ASCII字符“0”(即阿拉伯数字0)来填充原字符串的左侧,填充后新字符串案例的总长度取决于为width参数所分配的值。方法名称中的“z”即单词zero。
操作流程
步骤1:用字符“0”填充数字字符串。

步骤2:用字符“0”填充带负号(-)的数字字符串。

步骤3:其实,zfill方法可以用“0”填充任意字符串,例如含有字母的字符串。

步骤4:本案例代码执行结果如下:

案例26 对齐方式
导语
str类有三个案例方法,可用于对齐字符串,它们分别是:
· ljust方法,其中字母“l”是Left的意思,功能是使字符串左对齐。
· rjust方法,字母“r”是Right的意思,即右对齐。
· center方法,居中对齐。
以上三个方法都接收两个参数。为了能够计算字符串对齐后的位置,width参数是必需的,它提供经过处理后字符串的总长度。如果width参数小于或者等于原字符串的长度,那么这些方法会将原字符串返回(无须处理)。另一个参数fillchar是可选的,用来指定填充字符——字符串对齐后,剩余的空间由fillchar参数提供的字符填满,使总长度等于width参数的值。如果省略fillchar参数,默认使用空格填充。
尽管在格式化控制符中可以使用“<”“>”“^”等标志来设定字符串的对齐方式,不过,如果遇到要格式化的字符串本身包含这些标志符号时,使用格式化控制符就很难处理(无法对“<”“>”等字符进行转义),这种情形下也可以考虑改用ljust、rjust或center方法。
操作流程
步骤1:以下代码将字符串左对齐,总长度为25,并用“<”符号填充。

步骤2:右对齐,总长度为25,用“>”符号填充。

步骤3:居中对齐,用“#”号填充。

步骤4:以上代码的运行结果如下。

案例27 查找子字符串
导语
在字符串中查找子串位置的案例方法主要是find,它的原型如下:

sub参数指定要在原字符串中查找的子字符串。start与end参数限定在原字符串中的查找范围。这两个参数是可选的,如果省略,默认会在整个字符串中查找子串。如果原字符串中找不到sub参数提供的子串,find方法返回-1;如果找到,返回子串在字符串中的位置索引(索引从0开始计算)。
另外,还有一个rfind方法,其用途与find方法相同,不同的是:find方法是从左向右进行查找的,而rfind方法是从右向左查找的,两者只是查找方向不同。
操作流程
步骤1:声明一个变量,并用字符串表达式赋值,稍后会在该字符串中分别查找两个“月”字的位置。

步骤2:依次调用find和rfind方法,从两个方向(从左到右,从右到左)查找字符串中的“月”。

步骤3:向屏幕打印查找结果。

步骤4:案例运行结果如下:

案例28 startswith与endswith方法
导语
这两个方法的原型签名是相同的。

prefix指定要查找的子字符串,startswith判断原字符串是否以prefix所指定的子字符串开头,而endswith方法则相反,判断原字符串是否以prefix所指定的子字符串结尾。如果判断成立就返回True,否则返回False。start和end是可选参数,用于限制查找范围,如果省略,即在整个字符串中查找。
操作流程
步骤1:声明并初始化一个变量,然后判断其是否以“ca”开头。

步骤2:再声明一个变量,判断一下它是否以“de”结尾。

步骤3:运行本案例,得到的结果如下:

案例29 统计子字符串出现的次数
导语
count方法的原型如下:

count方法的功能是统计子字符串sub在父字符串中出现的次数。start与end参数是可选的,表示统计的范围。count方法返回一个整数值,即子字符串sub出现的次数,如果父字符串中没有找到匹配的子串,count方法将返回0。
操作流程
步骤1:统计以下字符串中字母“a”出现的次数。

步骤2:统计汉字“一”在字符串中出现的次数。

步骤3:以上代码运行后,输出的内容如下:

案例30 文本的标题样式
导语
capitalize方法和title方法都能将字符串切换为“标题”样式。capitalize方法仅仅把字符串中第一个单词的首字母改为大写,其余单词皆为小写字母;而title方法则会把字符串中每个单词的首字母都改为大写。
操作流程
步骤1:以下为待处理字符串。

步骤2:调用capitalize方法转换标题样式,并输出到屏幕上。

步骤3:调用title方法将字符串案例转换为标题样式,并打印。

通过访问特殊成员__name__能获得方法案例的名称。
步骤4:运行以上代码,输出结果如下:

注意:capitalize和title方法对中文字符无效。
案例31 串联字符串
导语
join方法能够使用特定的分隔符,将一组字符串序列串联后以新的字符串案例返回。Python语言中join方法的使用与其他编程语言有些不同,str类的join方法是在分隔字符的案例上调用的。例如,要使用字符“&”将“ab”和“cd”两个字符串对象串联起来,join方法应该这样调用:

join方法接收iterable类型的参数(例如元组、列表等),上面代码中给参数传递了一个字符串列表对象,列表中包含两个字符串元素。串联后返回结果“ab&cd”。
操作流程
步骤1:声明变量,并用一个元组案例赋值。元组中包含四个字符串元素。

步骤2:用“|”字符把上面元组中的字符串进行串联,并打印。

步骤3:再声明一个变量,使用列表案例赋值。列表中包含三个字符串元素。

步骤4:使用字符“∗”对列表中的字符串元素进行串联。

步骤5:运行本案例,得到的输出结果如下:

案例32 拆分字符串
导语
split方法和rsplit方法都有拆解字符串的功能。split方法是常规处理,即字符串是从左到右进行拆解的;而rsplit方法则相反,是从右向左拆解字符串的。这两个方法的原型如下:

sep参数指定字符串的“拆分点”,随后将以sep参数所指定的字符为依据,将字符串进行分割,最终以序列的形式返回拆分后的字符串,并且去除掉sep参数所指定的字符。maxsplit参数控制拆解的次数,默认为-1,表示无限制。例如,要将字符串“xy+z+efg+dk”以“+”字符为分隔符进行拆分,将得到一个包含四个字符串元素的序列,它们分别是:“xy”“z”“efg”和“dk”。如果maxsplit参数设置为2,那么,拆分方法只对前两个“+”字符出现的地方进行拆分,并得到一个包含三个元素的序列,分别是:“xy”“z”和“efg+dk”。
操作流程
步骤1:初始化一个待处理的字符串变量。

步骤2:以“#”符号为分隔符对原字符串进行拆分。

步骤3:拆分字符串,但只拆分一次,即maxsplit参数为1。

步骤4:从右向左拆分字符串(调用rsplit方法),但限制只拆分两次,即maxsplit参数为2。

步骤5:运行以上代码,输出结果如下:

案例33 替换字符串
导语
replace方法可以在原字符串中查找目标子串,然后将其替换为新的内容。replace方法原型如下:

old参数指定要被替换的字符,new参数表示用于替换的新字符,count参数指定被替换的次数,默认值为-1,表示全部替换。假设要将某个字符串案例中的“at”替换为“on”,count参数指定为3。若原字符串中出现6处“at”子串,那么最后只有前面3个才会被替换为“on”,其余皆被忽略。
replace方法的参数列表以“/”结尾,表示调用replace方法时只能顺序传递参数,不能使用关键字传递参数。因此,以下调用将发生错误。

正确的调用方法如下

操作流程
步骤1:将字符串中的“水长”替换为“路远”。

步骤2:将字符串中的“abc”替换为“xyz”,而且只替换前两处“abc”(count参数为2)。

步骤3:本案例代码执行后输出结果如下:

注意:“abc_abc_abc”中“abc”只被替换两次,因此后面的“abc”被保留。
案例34 去掉字符串首尾的空格
导语
直接调用strip方法而不传递任何参数,就能够把字符串首部和尾部的空格去掉。strip方法也能去掉字符串首尾的制表符(Tab)和换行符。
操作流程
步骤1:去除字符串首尾的空格符。

在向屏幕打印结果时,在字符串案例周围加上了双引号是为了便于查看输出结果(空格、制表符、换行符在屏幕上是不可见的)。其中,\"转义字符并不是必需的,可以直接使用双引号,因为字符串的最外层使用了单引号包裹,里层允许直接使用双引号。
步骤2:去除字符串中的制表符。

步骤3:还可以去除字符串中的换行符。

步骤4:上述代码的执行结果为:

案例35 lstrip与rstrip方法
导语
与strip方法类似的,str类还公开了两个案例方法——lstrip方法和rstrip方法。lstrip方法只去除字符串首部的空格;相反地,rstrip方法用于去除字符串尾部的空格。
操作流程
步骤1:调用lstrip方法去除字符串首部的制表符。此方法只处理字符串首部位置,如果字符串尾部存在制表符,是不会被去除的。

返回的新字符串应为“Hello Tom\t”,保留尾部的“\t”。
步骤2:调用rstrip方法,去除字符串尾部的空格。

rstrip方法去除字符串尾部字符,不会修改首部,不过,因为上面定义的字符串首部没有出现空格,所以此处调用rstrip方法与调用strip方法的效果相同。
步骤3:去除字符串首尾的空格。strip方法对字符串的首部和尾部都会进行处理。

步骤4:将上述各段代码的处理结果打印到屏幕上。

步骤5:运行本案例后,屏幕输出结果如下:

案例36 去除字符串首尾的特定字符
导语
strip、lstrip和rstrip方法都有一个可选chars参数,允许指定一组字符。调用方法后,会在原字符串中查找chars参数中提供的字符,如果找到,并且这些字符位于原字符串的首部或尾部,那么这些字符就会被去除。例如

strip方法的参数中提供了三个字符:c、h、b。字符串“batch”的首字符是“b”,此字符存在chars参数中,所以字符“b”被去掉,原字符串变为“atch”;接着,原字符串中尾部字符是“h”,此字符也存在于chars参数中,因此被去除,字符串变为“atc”;随后,字符串的尾部字符变成“c”,也能在chars参数中找到,所以字符“c”也被去除。最终,得到的字符串为“at”。
操作流程
步骤1:去除字符串首部和尾部的“=”。

尽管字符串首尾有多个“=”字符,但调用strip方法时只需要指定一个“=”字符即可,strip方法会对原字符串进行循环“剥离”,直到其首部和尾部找不到“=”字符为止。
步骤2:去除字符串首部的“#”和“$”字符。此处只需调用lstrip方法即可。

步骤3:去除域名中的“www.”前缀和“.com”后缀。

此处不能调用strip('wcom.')来处理,因为如果前缀的“www.”被去除后,“cctv”首部有两个“c”字符,这样会导致这两个“c”字符也被意外地去除了。所以,这里可以分两步处理:第一步先用lstrip方法把域名前面的“www.”去掉;第二步调用rstrip方法将域名尾部的“.com”去除。最终保留字符串“cctv”。
步骤4:案例代码执行后输出的结果如下:
