#字符串的索引和切片a ="disDisdj"a1 = a.capitalize()#首字母大写print(a1)a2 = a.upper()#全大写a3 = a2.lower()#全小写print(a2 , a3)a="hsjakjHSH"b=input("请输入验证码:")#不区分大小写if a.upper() == b.upper(): print("输入成功")#大小写反转a4=a.swapcase()print(a4)a="shajsh jhadasdh dhjsadh"#中间可以用特殊字符或者数字隔开a12=a.title()print(a12)#居中.空白填充a = "dhsakhdkd"a6 = a.center(20,"@")print(a6)#空格补足前八位八位s="hda/tdakj"s1=s.expandtabs()print(s1)计算字符长度s=" dbjkas kjalal"#前后中间的空格也算字数l=len(s)print(l)判断是否以“~”为开头或结尾s = "ssdhkasdlk"# s1=s. startswith("ssdh")# s2=s.startswith("d",2 ,6)s3=s.endswith("k")print(s3)print(s1 , s2) 通过元素找索引,找不到返回-1s="ssdhkasdlk"s1=s.find("d")#找到并定位s2=s.find("x")#找不到输出-1s3=s.index("z")#找不到系统会报错print(s3)print(s1,s2)从前后或前后同时同时删除空格和符号strip rstrip lstrips=" %hsdadadkd& "s1=s.strip(" %&")print(s1)数有几个字母counts = " %hsdadadkd& "s1=s.count("d")#没有输出0print(s1)分割splits = "%hsd adad kd&"s1=s.split()#以空格分割s2=s.split("d")#以d分割#被分割元素消失print(s1,s2)格式化输出 format三种形式s="我叫{},mu name is {},my name is {}".format("shkshk","jsahs","akhdka")print(s)s1="my name is {0},his name is {1},her name is {2},again my name is {0}".format("gshaaj","dadd","sdada")print(s1)s3="i am {name}, i am {age}, i like {hobby},again i am {name}".format(name ="sassas",age="12",hobby="bassketable")print(s3)替换replaces="jdshasjdalkjdlad;sdasd'd['aps["s1=s.replace("s","r",3)#替换三次replace(old. new, 次数)s2=s.replace("s","r")#默认全部替换print(s1 ,s2)***is 判断是什么print(name.isalnum())#由字母和数字组成print(name.isalpha())#由字母组成print(name.isdigit())#由数字组成for循环 循环内部所有元素s="hsadgakjdhldaiskdsjdsai"for i in s: print(i)in/not in 的用法s="hsadgakjdhldaiskdsjdsai"if "gaka" not in s: print("重新输入")if "gak" in s: print("重新输入")#数据#分类 int 12344计算 bool: true or false str: "张三""看来""saskajsha""saksak1728192" list:[1,2,3,4,"泰泰","23456"[0,0,7,8,8]] 元组:只读 dict:字典{"name":"张三","age"="12"} 字典{"name":[1,2,,4,5,],"age":[12,45,45,45,22]}关系型极强 集合{1,3,5,6,"jaksha"}#int操作方法#int操作方法#int操作方法#测数字转换成二进制最小位数bit_lengthi=2print(i.bit_length())#转换成二进制的最小位数#bool值 true false#str---int #此处str只能为数字#int---str #都行#int--booli=3b=bool(i) #0为false,非0为trueprint(b)#bool---int #true为1 ,false为0while 1: = whlie true:#str---bool #""(空字符串)为false,非空字符串就为truesif s: print("jslajks")else: print("hsjkahsjk")字符串的的索引和切片字符串的的索引和切片s="AAKJADL;AJDLSAJ;LFIHDFDND"#索引s1=s[0]s2=s[3]s3=s[-2]#取倒数第二位print(s1,s2)#切片 #顾头不顾尾#s="AAKJADL;AJDLSAJ;LFIHDFDND"s3=s[1:3]#取第二个到第三个[首+1:尾]print(s3)s4=s[0:-3]#第一个取到倒数第四个[首+1:尾-1]print(s4)s5=s[0:]print(s5)s6=s[0:0]#空print(s6)s7=s[0:6:3]#正着取[首+1:尾:步长-1]print(s7)s8=s[-1:-4:-1]#[首:尾+1:步长]print(s8)s9=s[4:0:-1]print(s9)s10=s[4::-1]print(s10)s11=s[::-1]print(s11)s12=s[-4:-1:1]print(s12)