name1="yq cc py th wx pp" print(name1.count("yq")) print(name1.count("yq",0,10)) mystr1="my python i love " print(mystr1.replace("python","java")) #切分 print(name1.split(" ")) name2=name1.split(" ") print(name2) print(", ".join(name2)) biddata=" -----data structure---- " #去掉字符串的两边的空格 print(biddata.strip("--").strip()) name1="yq cc py th wx pp" print(name1.startswith("Yq")) # 以神什么开头是则返回True print(name1.startswith("yq"))# 以神什么开头是则返回flase print(name1.startswith("yq")) # 以神什么结尾是则返回True print(name1.startswith("yq"))# 以神什么结尾是则返回flase
# 索引 name=["yq", "wx", "py", "yq"] print(name[1]) print(name[0]) print(name[2]) # 查早指定字符返回索引 print(name.index("wx",0,2)) # 计算字符出现·的次数 print(name.count("yq")) # 计算列表的元素个数 print(len(name)) # 判断指定字符是否在列表中,是True 否是false print("yq" in name) print("th" in name) print("pp" not in name) # 向列表中添加元素 # 有灰色下划线表示不符合PEP8规范 name2= ["yq","wx","py","yq"] name2.append("pp") print(name2) # 往列表中添加列表 name2.append(["zb","cc","th"]) print(name2) #将列表中的元素一个一个拿出来添加 添加到name2 name2 = ["yq","wx","py","yq"] name2.extend(["zb", "th"]) print(name2) name2 = ["yq","wx","py","yq"] name2.insert(1, "pp") print(name2) list1 =["1", 2, 3] #删除整个列表 del list1 #print(list1) list2=["1", 2, 3] del list2[0] print(list2) lis3 =[3, 6, 9] # lis3弹出一个元素 默认最后一个返回被弹出的值 result = (lis3.pop(0)) print(result) print(lis3) # 删除指定的一个元素 list4=[3, 6, 9, ] list4.remove(9) print(list4) #清空列表 list4.clear() print(list4) # 修改指定位置的数 list5 =[3, 6, 8, 9, ] list5[3] = 12 print(list5) # 从小到大排序 num_list = [6, 3, 2, 8, 7, 23] print(num_list.sort()) print(num_list) # 从大到小排序 num_list.sort(reverse=True) print(num_list) num_list = [6, 3, 2, 8, 7, 23] num_list2 = num_list.copy() print(num_list2) names =['yw', 'er', 're', 'df', '22'] for name in names: print(name) names =['yw', 'er', 're', 'df'] i = 0 while i < len(names): print(names[i]) i += 1 #列表的嵌套 names = ['yq', 'wx', 'py', 'yq', 'pp', ['zb', 'cc', 'th']] print(names[5][1])