字符串
name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 '---------------------Alex Li----------------------' name.count('lex') 统计 lex出现次数 name.encode() 将字符串编码成bytes格式 name.endswith("Li") 判断字符串是否以 Li结尾 "Alex\tLi".expandtabs(10) 输出'Alex Li', 将\t转换成多长的空格 name.find('A') 查找A,找到返回其索引, 找不到返回-1 format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22' format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22' msg.index('a') 返回a所在字符串的索引 '9aA'.isalnum() True '9'.isdigit() 是否整数 name.isnumeric name.isprintable name.isspace name.istitle name.isupper "|".join(['alex','jack','rain']) 'alex|jack|rain' maketrans >>> intab = "aeiou" #This is the string having actual characters. >>> outtab = "12345" #This is the string having corresponding mapping character >>> trantab = str.maketrans(intab, outtab) >>> >>> str = "this is string example....wow!!!" >>> str.translate(trantab) 'th3s 3s str3ng 2x1mpl2....w4w!!!' msg.partition('is') 输出 ('my name ', 'is', ' {name}, and age is {age}') >>> "alex li, chinese name is lijie".replace("li","LI",1) 'alex LI, chinese name is lijie' msg.swapcase 大小写互换 >>> msg.zfill(40) '00000my name is {name}, and age is {age}' >>> n4.ljust(40,"-") 'Hello 2orld-----------------------------' >>> n4.rjust(40,"-") '-----------------------------Hello 2orld' >>> b="ddefdsdff_哈哈" >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则 True
字典 键值对 key-value的数据类型
info ={ 's001':"adb", 's002':"adb", 's003':"adb", 's004':"adb", }
字典的特性:
dict是无序的key
必须是唯一的,不能重复
增加
info['s005'] = "bbg"
修改
info["s001"] = "ccd" info
删除
info.pop("s001") #删除 del info ["s002"] #删除 info.popitem() #随机删除
查找
“s001” in info #标准 info.get("s002")#获取 info["s003"]#如果没有就会报错,get不会
多级字典嵌套及操作
catalog = { "欧洲":{ "www.baidu.com": ["很多免费的,也很大","还行"] }, "亚洲":{ "www.seafir.com":["质量怎样不清楚","收费的"] }, "非洲":{ "www.ssss.com":["全部免费","服务器在国外,慢"] } } av_catalog["非洲"]["www.ssss.com"][1] += ",可以用爬虫爬下来" print(av_catalog["非洲"]["www.ssss.com"]) #ouput ['全部免费', '服务器在国外,慢,可以用爬虫爬下来']
操作
info.value() #值 info.keys() #键 info.update() #更新合并 info.items() #遍历键值 #方法1 for key in info: print(key,info[key]) #方法2 for k,v in info.items(): #会先把dict转成list,数据里大时莫用 print(k,v)