字符串
Posted qwer-123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串相关的知识,希望对你有一定的参考价值。
#全大写,全小写 s="1,fff,zc,33" s1=s.upper() s2=s.lower() print(s1,s2) """ s3="sdbi" s4=input("输入验证码,不区分大小写") if s3.upper()==s4.upper(): print("验证通过") else: print("重试")""" #首字母大写 s5="vds" s6=s5.capitalize() print(s6) #大小写反转 s7="csbUJ" s8=s7.swapcase() print(s8) #每个隔开的(特殊字符或数字)的单词首字母大写 s9="abh ji$ nn55jbk" s10=s9.title() print(s10) #居中,空白填充 s11="sna" s12=s11.center(12,"f") print(s12) #公共方法 s13="sdvsgz651" s14=len(s13) print(s14) #以什么开头结尾 s15="sc645" s16=s15.startswith("c",2,3) print(s16) if s16: pass elif s15.startswith("lk"): pass #通过元素找索引,找不到返回-1 s17="csscc55" s18=s17.find("9") print(s18,type(s18)) """ #通过元素找索引,找不到报错 s19="scaa44" s20=s19.index("5") print(s20)""" #默认删除前后空格 s21=" gh%$ njk " s22=s21.strip("%") print(s22) """ s23=input("输入").strip() if s23=="你好": print("恭喜")""" #字符串拆分列表 s24="svcds55" s25=s24.split("c") print(s25) #format 格式化输出 s26="my name is {},my age is {},my hobby is {}".format("xie",22,"girl") print(s26) s27="my name is {0},my age is {1},my hobby is {2}".format("xie",22,"girl") print(s27) s28="my name is {name},my age is {age},my hobby is {hobby}".format(age=22,name="xie",hobby="girl") print(s28) name=input("输入名字") s29="my name is {name},my age is {age},my hobby is {hobby}".format(age=22,name=name,hobby="girl") print(s29) #replace s30="哈哈哈,我笑了" s31=s30.replace("哈","呵",1) print(s31) #for循环 s="sdxa555" for i in s: print(i) if "d" in s: print("好") #bit_length i=100 j=i.bit_length() print(j) # s=bool("") print(s) s31=bool(" ") print(s31) # s if s: print("空") else: pass # s32="asc545" s33=s32[1]+s32[3] print(s33) # s34="asc455" for i in s34: print(i) s34="asc455" index=0 while 1: print(s34[index]) index+=1 if index==len(s34): break #如content=input("输入内容"),如用户输入5+9,5 +9,然后进行分割再进行运算 content=input("输入内容").strip() con1=content.split("+") num=0 for i in con1: num+=int(i) print(num) content=input("输入内容").strip() index=content.find("+") a=int(content[0:index]) b=int(content[index+1:]) print(a+b) # count=0 s=input("输入") for i in s: if i.isdigit(): count+=1 print(count)
以上是关于字符串的主要内容,如果未能解决你的问题,请参考以下文章