一 find:
find主要是检测str 是否含在列表中,如果在返回开始的索引值,否则返回 1
##list =" wo shi ge hao ren"
list.find("wo")
0
list.find("shu")
-1
二 Index:
## 根find()的用法一样,不同点在于如果这个str不在这个列表中时会报错
list
"wo shi ge hao ren"
list.index("wo")
1
list.index("cai")
这个会报错内容为:没有找到子字符串。
三 count:
能返回str 在开始和结束之间在列表中出现的次数
list
"wo shi ge hao ren"
list.count("wo")
2
四 replace:
list中的str1替换成str2
list
"wo shi ge hao ren"
list.replace("wo","ni")
"ni shi ge hao ren"
五 split:
以str为分隔符切片list,如果maxsplit有指定值,则仅分隔maxsplit个子字符串
list.split(" ")
['wo',shi',ge',hao',ren']
list.split(" ",2)
['wo', 'shi', ge hao ren']
六 capitalize:
把字符的第一个字符大写
list
"wo shi ge hao ren"
list.capitalize()
"Wo shi ge hao ren“
七 title:
把字符串的每个单词首字母大写
list
"wo shi ge hao ren"
list.title()
"Wo Shi Ge Hao Ren"
八 startswith
检查字符串是否以**开头,是则返回True,否则返回False
list
"wo shi ge hao ren"
list.startswith("w")
True
九endwith
##这个与startswith相反,检查的是以**结尾
list
"wo shi ge hao ren"
list.endwith("n")
True
十lower
转换列表中的所有大写字符为小写
list
"Wo shi ge hao ren"
list.lower()
"wo shi ge hao ren"
十一upper
转换列表中的大小写为大写
list
"wo shi ge hao ren"
list.upper()
"WO SHI GE HAO REN"
十二ljust
##返回一个原字符串左对齐,并使用空格填充至长度width的新字符串
list
"wo shi ge hao ren"
list.ljust(50,"*")
"wo shi ge hao ren***********************"
list.ljust(50)
"wo shi ge hao ren "
十三 rjust
用法与ljust相反
list
‘wo shi ge hao ren"
list.rjust(50,"*")
****wo shi ge hao ren"
list.rjust(50)
wo shi ge hao ren"
十四 center
返回一个原字符串居中,并使用空格填充至长度width的新字符串
list
"wo shi ge hao ren"
list.center(50,"*")
*** wo shi ge hao ren ***"
list.center(50)
wo shi ge hao ren "
十五 Istrip
删除列表左边的空白字符
list = " wo shi ge hao ren"
list.istrip()
wo shi ge hao ren"
十六rstrip 与strip
rstrip删除list 字符末尾的空白字符 strip是删除list字符俩端的空白字符
list.strip()
list = " wo shi ge hao ren "
list.strip()
"wo shi ge hao ren"
list ="wo shi ge hao ren "
list.rstrip()
"wo shi ge hao ren"
十七 partition 与rpartition
partition把列表yistr分为3部分 str前
str, str,str后,rpartition与partition相似不过是从右边开始 的
list()
"wo shi ge hao ren"
list.partition("ge")
"wo shi ","ge " ,"hao ren"
list
"wo shi ge hao ren"
list.rpartition("hao")
"wo shi ge","hao","ren"
十八 splitlines
按照行分隔,返回一个包含各行作为元素的列表
list.splitlines()
list
"wo shi\ ge\n hao ren"
list.splitlines()
["wo shi","ge" ,"haoren"]
十九isalpha,isdigit,isalnum,isspace的用法及区别
isalpha如果列表所有的字符都是字母则返回True,否则返回false
isdigit如果列表只包含数字则返回Ture否则返回false
isalnum如果列表所有字符都是字母或数字则返回Ture,否则返回false
isspace如果列表中只包含空格,则返回Ture,否则返回false
list = "wo shi ge hao ren"
list.isalpha()
True
list = "123456"
list.isdigit()
Ture
list = "123456"
list.isalnum()
Ture
list = "1234abcd"
list.isalnum()
Ture
list = "123"
list.isspace()
false
list = " "
list.isspace
Ture
二十join
在列表中每个字符后面插入str,构造出一个新的字符串
list = "wo shi hao ren"
str = :1234"
list.join(str)
"wo1 shi 2 hao3 ren4"
网友评论