Week2-列表字符串方法示例
Posted pythonlee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Week2-列表字符串方法示例相关的知识,希望对你有一定的参考价值。
#!/usr/bin/en python # Author:lijun import copy names = ["lijun","haha","shanshan","heihei"] #增删改查 print(names[0],names[2]) print(names[1:3]) print(names[-2:0]) print(names[-2:]) print(names[:3]) names.append("xixi") print(names) names.insert(1,"gaga") print(names) names[2] = "22" print(names) names.remove("lijun") print(names) del names[1] #names.pop(1) print(names) names.pop() #默认删除最后一个 print(names) #列表索引位置: print(names.index("shanshan")) print(names[names.index("shanshan")]) #清空列表 #names.clear() #print(names) #反转列表: names.reverse() print(names) #排序 names.sort() print(names) #合并列表: name2=[1,2,3,4] names.extend(name2) print(names) print(name2) #删除列表: #del name2 #print(name2) #复制列表:浅copy(只拷贝第一层): name2=names.copy() #等于name2=copy.copy(names) name2=names[:] name2=list(names) print(name2) #拷贝不变 names[2] = "李军" print(names) print(name2) #列表内包括列表:拷贝只拷贝最外一层,列表内的列表只是内存指针,拷贝会跟随改变 names.append([1,2]) name2=names.copy() #深copy: name3 = copy.deepcopy(names) print(names,name2,name3) names[2]="lijun" names[-1][0]=3 print(names,name2,name3) #遍历列表:range(5) for i in names: print(i) for i in range(5): print(names[i]) name4="my name is lijun" print(name4.capitalize()) #首字母大写 print(name4.count("i")) #统计字母i的数量 print(name4.center(50,"=")) print(name4.endswith("un")) #判断结尾 print(name4.expandtabs(tabsize=30)) print(name4.find("name")) #索引位置 print(name4[name4.find("name"):]) #切片 name5="my name is {name} and i am {year} old" print(name5.format(name=‘shanshan‘,year=23)) print(name5.format_map({‘name‘:‘shanshan‘,"year":"23"})) print(‘ab23‘.isalnum()) #检测字符串是否由字母和数字组成。 print(‘abA‘.isalpha()) print("1A".isdecimal()) print("1A".isdigit()) print("1A".isidentifier()) #判断是不是一个合法的标识符(变量名) print("a 1A".islower()) #是否为小写 print(‘1‘.isnumeric()) #是否是数字 print(‘ ‘.isspace()) print(‘My Nmae Is ‘.istitle()) print(‘My Nmae Is ‘.isprintable()) print(‘My Nmae Is ‘.upper()) print(name4.ljust(50,‘*‘)) print(name4.rjust(50,‘*‘)) print("LIJUN".lower()) print("lijun".upper()) print(" lijun".lstrip()) #去掉左边换行符 print("lijun ".rstrip()) #去掉右边换行符 print(" lijun ".strip()) #去掉前后换行符 print(‘,‘.join([‘1‘,‘2‘,‘3‘,‘4‘])) #以,为分隔符将列表转换成字符串 p = str.maketrans("abcdef",‘123456‘) print("alex li".translate(p)) #根据上面规则加密 print("lijun li".replace(‘l‘,‘L‘)) print("lijun li".replace(‘l‘,‘L‘,1)) print("lijun li".rfind(‘l‘)) #找出最右边字母l的索引位置 print("lijun li".split(‘l‘)) #按l分隔字符串,默认按空格 print("1+2+3+4".split(‘+‘)) print("1+2 3+4".splitlines()) print("LiJun".swapcase()) print("li jun".title()) print(‘li jun‘.zfill(50)) #总共50位,不够用0补充 用于进制
以上是关于Week2-列表字符串方法示例的主要内容,如果未能解决你的问题,请参考以下文章