1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Auther: Summer 4 5 # 字符串操作 6 name = "summer Du" 7 print(name.capitalize()) # 首字母变大写 8 print(name.casefold()) # 大写字母全部变小写 9 print(name.center(50,"=")) # 输出====================summer Du===================== 10 print(name.count("m")) # 统计m出现次数 11 print(name.encode()) # 将字符串编码成bytes格式 12 print(name.endswith("m")) # 判断字符串是否以m结尾 13 print(name.find("D")) # 查找D,找到返回其索引值,找不到返回-1,返回其找到第一的索引值 14 15 name = "sum\tmer Du" 16 print(name.expandtabs(10)) # 输出sum mer Du 17 18 # format 19 msg = "my name is {},and age is {}" 20 print(msg.format("summer","24")) 21 msg = "my name is {1},and age is {0}" 22 print(msg.format("summer","22")) 23 msg = "my name is {name},and age is {age}" 24 print(msg.format(name = "summer",age = "22")) 25 print(msg.format(age = "22",name = "summer")) 26 27 # format_map 28 print(msg.format_map({‘age‘: 22,‘name‘: "summer"})) 29 msg = "my name is {name},and age is {age}" 30 print(msg.index("m")) 31 32 print("9Aa".isalnum()) 33 print("9".isdigit()) # 是否整数 34 print(name.isnumeric()) 35 print(name.isprintable()) 36 print(name.isspace()) 37 print(name.istitle()) 38 print(name.isupper()) 39 name = "summer Du" 40 print(name.join("lee")) 41 print("|".join(["lee","summer"])) # 放到列表里 42 43 # maketrans Python maketrans() 方法用于创建字符映射的转换表, 44 # 对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符, 45 # 第二个参数也是字符串表示转换的目标。 46 intab = "aeiou" 47 outtab = "12345" 48 trantab =str.maketrans(intab,outtab) 49 print(trantab) 50 51 # partition 52 print(msg.partition("is")) 53 54 r = "my name is summer,and age is 22" 55 print(r.replace("m","n",2)) 56 57 print(name.swapcase()) # 大小写互换 58 59 print(msg.zfill(40)) 60 print(msg.ljust(40,"-")) 61 print(msg.rjust(50,"=")) 62 print(msg.center(50,"=")) 63 64 b = " ddefs;ajfdal;_hahO(∩_∩)O哈哈~" 65 c = "dd_哈哈" 66 print(b.isidentifier()) ##检测一段字符串可否被当作标志符,即是否符合变量命名规则 67 print(c.isidentifier()) ##检测一段字符串可否被当作标志符,即是否符合变量命名规则