#__author:"Feng Lin"#date:2018/8/ -#字符串操作s='Sddasdw11234wiu'#capitalize首字母大写s1=s.capitalize () print (S1) S2=S.upper () print (s2) S3=S.lower () print (S3) # #实例验证码不区分大小写 # S4="AcEB"# You_input=input ("Please enter the verification code:")# ifS4.upper () = =You_input.upper (): # Print ("Succee")# Else: # Print ("Error") # #大小写反转s4=s.swapcase () print (S4) #每个隔开的单词首字母大写na='Alex Egon Wusier wjj'N1=Na.title () print (n1) TT='Lin_feng*ai-nvren'TT1=Tt.title () print (TT1) s='Alexwwewsdad'#居中, the default space padding, or you can specify the fill character S6=s.center ( -) Print (S6) S7=s.center ( -,"$") Print (S7) #自动扩展字符串中的tabs='Alex\tsssir'S1=s.expandtabs () print (S1) #公告方法print (Len (s)) #判断是否以某个字符开头或结尾, returns the BOOL value s='bbbaaacccddd'S8=s.startswith ("b") S9=s.endswith ("DDD") print (S8) print (S9) #find通过元素找索引, cannot find the return-1s='Alexwulitaotao'S1=s.find ('Wuli') print (S1) S2=s.find ('D') print (s2) #strip默认删除前后空格s="Babablaala"S1=S.strip () print (S1) S1=s.strip ("a") print (S1) #lstrip默认删除左边空格, Rstrip remove the right space by default S2=S.lstrip () print (s2) S3=S.rstrip () print (S3) #计算字符串中元素的个数s="What fuck!"Num=s.count (" at") print (num) num=s.count (" at") print (num) #自定义分割字符串s="lin:feng:hao:shuai:!"L=s.split (":") Print (l) #format格式化输出三种方法s='My name is {},my. {},my Hobbies is {}'. Format ("Linfeng", -,"Gril") print (s) s='My name is {0},my. {1},my Hobbies is {2}'. Format ("Linfeng", -,"Gril") print (s) s='My name is {name},my-age},my hobbies is {hob}'. Format (age= -, hob="Gril", name="Linfeng") print (s) #replace替换字符串内容, and finally you can specify how many s to modify="I swear to you, Caesar , I've dug up the Ava of Isaac."S1=s.replace ("I'm","You're") print (S1) S2=s.replace ("I'm","You're",1) print (s2) #is判断字符组成name="sadadsdads1233144"#是否只由数字组成, returns the BOOL value print ( name.isdigit ()) #是否只由字母组成, returns the BOOL value print (Name.isalpha ()) #是否由字母和数字组成, returns the BOOL value print ( Name.isalnum ())
Python Basic Learning-string