字符串
切割
split(str=””[[,num])
(以str为分隔符截取字符串,指定num,则仅仅截取num个字符串)
eg:str = “sunck**is****a*good**man”
print(str.split(“*”)) 返回['sunck', '', 'is', '', '', '', 'a', 'good', '', 'man']
print(str.split(“*”,3)) 返回['sunck', '', 'is', '***a*good**man']
splitline([keepends]})
(按照’\r’,’\n’,’\r\n’分隔,返回。keepends=Ture时会保留换行符,默认为False)
eg:str = ‘’’sunck
is
a
man‘’’
print(str.splitline()) 返回['sunck', ' is', ' a', ' man']
print(str.splitline()) 返回['sunck\n', ' is\n', ' a\n', ' man']
组合
join(seq)
(以指定的字符串分隔符,将seq中的所有元素组合成一个字符串)
eg:list = [‘sunck’,’is’,’good’]
str = “**”,join(list)
print(str) 返回sunck**is**good
max() min()
eg:str = “sunck is a good man”
print(max(str)) 返回u
print(“*”+min(str)+”*”) 返回* *
replace(oldstr,newstr,count)
(用newstr替换oldstr,默认是全部替换。如果制动了count,那么只替换前count个)
eg:str = “sunck is a good man good”
print(str.replace(“good”,”nice”) 返回sunck is a nice man nice
print(str.replace(“good”,”nice”,1) 返回sunck is a nice man good
maketrans()
(创建一个字符串映射表)
eg:t = str.maketrans(“good”,”2345”)
t1 = str.maketrans(“abc”,”ABC”)
str = “sunck is a good man good”
print(str.translate(t)) 返回sunck is a 2345 man2345
print(str.translate(t1)) 返回sunCk is A good mAn good
startwith(str[,start=0][end=len(str)])
(在给定的范围内判断是否已给定的字符串开头,如果没有指定范围,默认整个字符串长度)
eg: str = “sunck is a good man good”
print(str.startwith(“sunck”)) 返回True
print(str.startwith(“sunck”,5,16) 返回False
endwith(str[,start=0][end=len(str)])
(在给定的范围内判断是否已给定的字符串结尾,如果没有指定范围,默认整个字符串长度)
eg: str = “sunck is a good man good”
print(str.startend(“good”)) 返回True
encode(encoding=”utf-8”,errors=”strict”)
编码
eg: str = “sunck is a good man good”
print(str.encode(“utf-8”)) 返回b'sunck is a good man good'
decode()
解码
eg: str = “sunck is a good man good”
data = str.encode(“utf-8”))
print(data.decode(“utf-8”)) 返回sunck is a good man good
isalpha()
(若字符串中至少与一个字符且所有的字符都是字母返回True,否则返回False)
eg:str = “sunck is a good man good”
print(str.islapha()) 返回True
isalnum()
(若字符串中至少与一个字符且所有的字符都是字母或者数字返回True,否则返回False)
eg:str = “sunck is a good man good 56481”
print(str.isalnum()) 返回True
isupper()
(若字符串中至少与一个英文字符且所有的英文字符都是大写的英文字母返回True,否则返回False)
eg:str = “sunck is a good man good 56481”
print(str.isupper()) 返回False
islower()
(若字符串中至少与一个英文字符且所有的英文字符都是小写的英文字母返回True,否则返回False)
eg:str = “sunck is a good man good 56481”
print(str.islower()) 返回True
istitle()
(若字符串标题化的(小驼峰(所有首字母大写))返回True,否则返回False)
eg:str = “sunck is a good man good 56481”
str1 = “Sunck Good ”
print(str.istitle()) 返回False
print(str1.istitle()) 返回True
isdigit()
(若字符串只包含数字字符返回True,否则返回False)
eg:str = “sunck is a good man good 56481”
str1 = “6165132132 ”
print(str.isdigit()) 返回False
print(str1.isdigit()) 返回True
isnumeric()
(同isdigit())
eg:str = “sunck is a good man good 56481”
str1 = “6165132132 ”
print(str.isnumeric)) 返回False
print(str1.isnumeric()) 返回True
isdecimal()
(若字符串只包含十进制字符返回True,否则返回False)
eg:str = “sunck is a good man good 56481”
str1 = “6165132132 ”
print(str.isdecimal()) 返回False
print(str1.isdecimal()) 返回True
isspace()
(若字符串只包含空格(空白字符也可以)返回True,否则返回False)
eg:str = “ ”
str1 = “\t\n\r\f”
print(str.isspace)) 返回True
print(str1.isspacec()) 返回True