- 字符串方法
expandtabs()字表符
#expandtabs ,遇见\t断句20字符,不够用空格填充 \n换行 test = 'username\temail\tpassword\nstar\[email protected]\t123\nstar\[email protected]\t123\nstar\[email protected]\t123' x = test.expandtabs(20) print(x)
结果 username email password star [email protected] 123 star [email protected] 123 star [email protected] 123
isalpha()判断是否为字母、汉字
#isalpha 判断字符串是否纯字母、汉字,返回真假 test = 'star22' v = test.isalpha() print(v)
isdecimal()、isdigit()判断是否是数字
#当前输入是否是数字 如果① isdigit能判断出来 test = '1233①' v = test.isdecimal() v2 = test.isdigit() print(v,v2) 结果:false true
isidentifier()判断是否为标识符
#判断是否为标识符字母、数字、下划线:标识符 def class,数字开头为false a = "11fdf" v = a.isidentifier() print(v) 结果:false
test = '1233①二' v = test.isdecimal() #只支持数字 v2 = test.isdigit() #特殊数字支持 v3 = test.isnumeric() #中文数字也支持 print(v,v2,v3)
结果:False False True
#s 是否存在不可显示的字符 #\t 制表符 #\n 换行 test = 'fdsjafkd\tsajfdsjf' v = test.isprintable() print(v)
结果:false
isspace() 是否全部为空格
#是否为全部空格 test = 'adfsd fsdaf' v = test.isspace() print(v)
结果:false
istitle()是否为标题
test = 'Star Star star' v = test.title() #将字符串转换为标题 v1 = test.istitle() #判断是否为标题,每个单词的首字母为大写 print(v,v1)
join()拼接字符串
#将字符串中的每个元素按照指定分隔符进行拼接 test = '你就是大傻逼' t = ' ' v = t.join(test) v1 = '——'.join(test) print(v) print(v1)
结果:你 就 是 大 傻 逼
你——就——是——大——傻——逼
ljusn()、rjust()、center()、zfill()填充
test = 'star' v1 = test.ljust(20,'*') v2 = test.rjust(20,'*') v3 = test.center(20,'*') v4 = test.zfill(20) #只能用0填充 print(v1) print(v2) print(v3) print(v4) 结果: star**************** ****************star ********star******** 0000000000000000star
islower() lower() isupper() upper()
test = 'Star' v1 = test.islower() #判断是否为小写 v2 = test.lower() #将字符转换为小写 print(v1,v2) test1 = 'hello' v3 = test1.isupper() #判断是否为大写 v4 = test1.upper() #将字符转换为大写 print(v3,v4) 结果: False star False HELLO
lstrip()、rstrip()、strip()去除空白或者指定的字符串
test = 'axstar ' # v1 = test.lstrip() #去除左侧空白 包括\t \n # v2 = test.rstrip() #去除右侧空白 # v3 = test.strip() #去除左右两边的空白 v4 = test.lstrip('axr') #去除指定字符,优先最多匹配 print(v4) 结果:star
maketrans()、translate()
test1 = 'DJY石头' test2 = 'ZMX傻子' v = 'DJY石头123ZMX傻子' print(v) s = str.maketrans(test1,test2) #建立对应关系 u =v.translate(s) #做一个转换 print(u) 结果: DJY石头123ZMX傻子 ZMX傻子123ZMX傻子
partition()、rpartition()以字符分割,最多只能分割三个
split()可以指定分隔个数,不包含分隔元素
test = 'starstarstar' v1 = test.rpartition('t') #结果:('starstars', 't', 'ar') v2 = test.partition('s') #以s分隔,最多分隔三份 结果:('', 's', 'tarstarstar') v3 = test.split('s',2) #['', 'tar', 'tarstar'] 根据是分隔,可以指定分隔个数,不包含分隔元素 v4 = test.rsplit('s') #['', 'tar', 'tar', 'tar'] print(v4)
splitlines()只能包含换行符分隔,true、false判断是否保留换行符
#只能包含换行符分隔,true、false判断是否保留换行符 test = 'fdsfsadf\nfdsafd\nfdsafdsf\n' v = test.splitlines(False) print(v) 结果: ['fdsfsadf', 'fdsafd', 'fdsafdsf']
startswith()、endswith()判断是否以规定的字符开头或者结尾
#是否以字符开头或者结尾 test = 'star' v1 = test.startswith('s') v2 = test.endswith('r') print(v1,v2)
#大小写转换 test = 'star' v = test.swapcase() print(v)
join、split、find、strip、upper、lower必须要会的
len()、for循环、索引、切片
test = 'star' #通过索引获取字符串中的某一个字符 v = test[3] #单个索引 print(v) #切片 v1 = test[0:1] #范围>=0 >1 print(v1) v2 = test[0:-1] #从头到最后,不包括最后一个 print(v2) test1 = [11,22,33,44,'aaa'] v3 = len(test) #字符串中有多少个字符 v4 = len(test1) print(v3,v4) a = '张大美女' index = 0 while index < len(a): v = a[index] print(v) index += 1 print('====') for zmx in a: print(zmx)