武汉13期吴德军 发表于 2022-4-18 19:00:52

python

索引#正向索引    0 1 2 3 4 5 6      #a = a b c d e f g #反向索引-7-6-5-4-3-2-1切片[起始索引位:结束索引位:步长值]切片语法中:包含起始不包含结束,步长值不能为0,可以为1(默认为1),1表示不隔位st ="abcdefghjkl"#print(st)print(st)#d 截取当前索引值为3对应的值print(st[-3])#j 截取当前索引值为-3对应的值print(st)#bcd 从索引值1开始取,到结束索引4,1-3有值print(st)#abc 从索引值0开始取,到结束索引3,0-2有值print(st[-4])#hprint(st)#cdefghjkprint(st[:-1])#abcdefghjkprint(st)#cdefghjklprint(st)#bcdefprint(st)#beprint(st)#bdfprint(st[::1])#abcdefghjkl 为空则取所有print(st[::-1])#lkjhgfedcba 把字符串的值进行反向输出print(st[-2:-5:-1])#kjhprint(st[-3:-6:-2])#jg起始索引位及结束索引位控制取值范围,步长值控制取值方向及隔位取值字符串字符串的定义用单引号或双引号类型为:stra ='asadad'#通过单引号定义一个字符串a1="adadada"#通过双引号定义一个字符串print(type(a))# 打印类型<class 'str'>print(type(a1))字符串中常用函数a ='asadad'#通过单引号定义一个字符串#对字符中首字母小写改为大写a1=a.capitalize()print(a1)#Asadadprint(a.capitalize())#Asadad#count统计字符串中元素出现的次数a ='asadad'#通过单引号定义一个字符串a1=a.count("a")#通过变量来接收值print(a1)#3print(a.count("a"))#3join 拼接st="hello"print("*".join(st))#h*e*l*l*ol=['h','e','l','l','o']print(l)#['h', 'e', 'l', 'l', 'o']print("".join(l))#hellosplit 分割st="he,llo" #定义一个字符串print(type(st))#<class 'str'>a1=st.split(',')#把st字符串中的多余的元素分割掉print(a1)#['he', 'llo']#注:通过split分割后的结果类型是list(列表)print(type(a1))#<class 'list'>print(''.join(a1))#helloprint(type(''.join(a1)))#<class 'str'>#删除字符st="hello"#定义一个字符串#strip:删除字符串的开头和结尾的元素#lstrip:删除字符串开头的元素,不能删除结尾的#rstrip:删除字符串结尾的元素,不能删除开头的print(st.strip("he"))#lloprint(st.strip("llo"))#heprint(st.lstrip('h'))#elloprint(st.rstrip("o"))#hell判断字符以什么开头或结尾, 返回的是布尔值st = "helloword"#定义一个字符串#startswith 判断是否以某某开头,是返回true 反之false#endswith 判断是否以某某结尾,是返回true 反之falseprint(st.startswith("he"))#Trueprint(st.startswith("e"))#Falseprint(st.endswith("rd"))#Trueprint(st.endswith("r"))#Falsest = "helloword"#定义一个字符串#find:获取指定值的索引值,从左开始#rfind:获取指定值的索引值,从右开始print(st.find("o"))#4print(st.rfind('o'))#6#替换函数st = "helloword"#定义一个字符串# print(st.replace("l","2"))#he22oword 默认替换指定值,所有# print(st.replace("l","3",1))#he3loword 把l换成3 1表示次数# print(st.replace("helloword","xiaoliu"))#把xiaoliu替换所有print(st.replace(st,"小刘"))#小刘 替换所有#把所有的大写字母变成小写st="HELLOword"#定义一个字符串print(st.lower())#helloword#把所有的小写字母变成大写st="HELLOword"#定义一个字符串print(st.upper())#HELLOWORD#判断是否全部为数字,返回布尔值 是true 反之falsest1='h12llo'#定义字符串st2="12345"#定义字符串print(st1.isdigit())#False 不满足全部为数字print(st2.isdigit())#True满足全部为数字#判断是否全部为全字母,返回布尔值 是true 反之falsest1='h12llo'#定义字符串st2="12345"#定义字符串st3='hello'#定义字符串print(st1.isalpha())#Falseprint(st2.isalpha())#Falseprint(st3.isalpha())#True#判断是否为全数字或全字母或数字字母组合,有特殊符合时为否返回布尔值st1='h12llo'#定义字符串st2="12345"#定义字符串st3='hello'#定义字符串st4="12h@h*"#定义字符串print(st1.isalnum())#Trueprint(st2.isalnum())#Trueprint(st3.isalnum())#Trueprint(st4.isalnum())#False#判断首写字母是否为大写,其他为小写st1="A"st2="AA"st3="A1234"st4="Adfd"st5="A2@"st6='ass'st7='a12'print(st1.istitle())#Trueprint(st2.istitle())#Falseprint(st3.istitle())#Trueprint(st4.istitle())#Trueprint(st5.istitle())#Trueprint(st6.istitle())#Falseprint(st7.istitle())#False#判断字符串开头是否都是大写或小写#isupper 判断全部为大写#islower 判断全部小写st1='sfdsgfhgfh' #定义字符串print(st1.isupper())#False判断是否全部为大写print(st1.islower())#True   判断是否全部为小写st2="asdasDDD"#定义字符串print(st2.isupper())#False判断是否全部为大写print(st2.islower())#False判断是否全部为小写st3='ASDSFRG'#定义字符串print(st3.isupper())#True判断是否全部为大写print(st3.islower())#False判断是否全部为小写st4 ='adsds12334'#定义字符串print(st4.isupper())#False判断是否全部为大写print(st4.islower())#True判断是否全部为小写st5='DSFDSF122342'#定义字符串print(st5.isupper())#True 判断是否全部为大写print(st5.islower())#False 判断是否全部为小写列表[]中括号来定义类型:list# list=['123']#定义列表# print(type(list))#<class 'list'># print(list)list1=list('123')print(list1)print(type(list1))a='2'#定义字符串print(type(a))#<class 'str'>c=list(a)#通过list把字符串变为列表print(type(c))#<class 'list'>list2=['hello',1,2,3,4,5,6]#列表中的每个元素都有一个索引print(list2)#helloprint(list2)#4list2 = ['hello', 1, 2, 3, 4, 5, 6]#定义列表list2='xiaoliu' #通过索引把对应索引的值进行替换print(list2)#['hello', 1, 'xiaoliu', 3, 4, 5, 6]#python3中可以直接打印中文,python2中不可以直接打印中文#往列表中添加元素a =#定义列表a.append("hello")#添加元素print(a)##列表的拼接a1 =#定义列表a2 =#定义列表a1.extend(a2)print(a1)##往列表指定索引位添加数据a1 =#定义列表a1.insert(2,"hello")print(a1)##查找列表中指定元素取对应的索引a1 =#定义列表print(a1.index(1))#0 查找列表中1这个元素的索引位为0#移除元素a1 =#定义列表a1.remove(1)#把列表中的1这个元素进行删除print(a1)##删除元素(索引值)a1 =#定义列表del a1#通过del 来删除索引值对应的数据print(a1)##升序排序a1=#定义一个列表a1.sort()#对当前这个列表进行升序排序print(a1)##降序排序a1=#定义一个列表#实现降序排序:Trueprint(sorted(a1,reverse=True))##实现降序排序:Falseprint(sorted(a1,reverse=False))##列表实现反转输出(面试常问)a1=#定义一个列表a1.reverse()#对列表进行反转print(a1)#a2 ="12345"#定义字符串print(a2[::-1])#54321 对字符串的反转a1=#定义一个列表a1.pop()#默认删除末尾的值print(a1)##查看删除的值a2=#定义一个列表print(a2.pop())#5 直接打印被删除的值a3=#定义一个列表a3.pop(0)#通过索引来删除对应的数据print(a3)#
页: [1]
查看完整版本: python