python3之字符串常用操作练习补充二
字符串常用操作练习补充二
1 #coding:utf-8 2 3 #***将"\t"转为空白符,默认使各段字符串长度为8的整数倍,指定长度则以指定的长度为准 4 str1 = "\tThis\tis Tab test\t!" 5 print(str1.expandtabs()) 6 ''' 7 输出结果为: 8 This is Tab test ! 9 |8字符 |8字符 |16字符 |! 10 ''' 11 print(16*"*") 12 13 14 #***查找字符串出现的位置 15 #find和index的区别为查找不到内容时index直接抛异常报错,find返回-1 16 #print(str2.index("lee")) 17 str2 = "This is find test,do you not how to use find?" 18 start1 = 0 19 while True: 20 index1 = str2.find("find",start1+len("find"),len(str2)-1) 21 if index1 != -1: 22 print(index1) 23 start1 = index1 24 else: 25 print("查找结束") 26 break 27 28 #***字符串的格式化拼接 29 str3 = "My name is {0},I'm {1}" 30 str4 = str3.format("Lee",30) 31 print(str4) 32 #两种拼接方法都是可以的,都比“+”的字符串拼接好 33 str5 = "Are you %s?" 34 str6 = str5 %("LiNing") 35 print(str6) 36 #这种方法让我想起了JSP 37 str7 = "His name is {name},he is {age}." 38 str8 = str7.format(name = "Daming",age = 28) 39 #和上面的类似,直接做成字典传入 40 str9 = str7.format_map({"name":"Jesson","age":18}) 41 print(str8) 42 print(str9) 43 44 print(16*"*") 45 #***字符串的各种判断 46 #是否为数字或字母,isalnum 47 print("aA".isalnum()) #True 48 print("5".isalnum()) #True 49 print("$".isalnum()) #False 50 print("Ⅴ".isalnum()) #True 51 #是否是数字0-9,isdigit 52 print() 53 print("aA".isdigit()) #False 54 print("5".isdigit()) #True 55 print("#".isdigit()) #False 56 print("Ⅵ".isdigit()) #False 57 #是否是数字isnumerice 58 print() 59 print("aA".isnumeric()) #False 60 print("5".isnumeric()) #True 61 print("^".isnumeric()) #False 62 print("Ⅵ".isnumeric()) #True 63 #是否为字母isalpha,a~z和A~Z为True 64 print() 65 print("a!dC".isalpha()) #False 66 print("AS".isalpha()) 67 #是否是十进制小数isdecimal 68 69 #是否为小写islower 70 71 #是否为大写isupper 72 73 #是否为空格isspace 74 print() 75 print("\t".isspace()) #True 76 print(" ".isspace()) #True 77 78 #是否为标题istitle 79 80 print(16*"*") 81 82 #***分隔符的操作 83 list1 = ["E","x","c","e","p","t","i","o","n"] 84 print("".join(list1)) #Exception 85 print("-".join(list1)) #E-x-c-e-p-t-i-o-n 86 87 print(16*"*") 88 #***左右填充 89 print("253".ljust(12,"$")) 90 print("253".rjust(12,"$")) 91 list2 = ["251","5","35","4567"] 92 #数据靠右对齐 93 for val in list2: 94 print(val.rjust(6," ")) 95 96 print(16*"*") 97 #***只去除左边/右边的空格 98 #lstrip()/rstrip() 99 100 #字符串的对应表 101 intab = "abcde" 102 outab = "12345" 103 #将abcde对照为12345 104 trantab = str.maketrans(intab,outab) 105 str10 = "adagsdjfglamxcvb" 106 #利用对照表trantab对str10进行字符替换 107 str11 = str10.translate(trantab) 108 print(str11) #141gs4jfgl1mx3v2 109 110 print(8*"*") 111 #***字符串的分割 112 print() 113 str12 = "My name is Lee" 114 print(str12.partition("is")) 115 #print(str12.rpartition("is")) 116 117 #字符串的替换,转换个数2可选 118 print(str12.replace("e","8",2)) 119 120 print(16*"*") 121 print() 122 #888根据行进行分割,注意,第一行前面有一个回车符,也算一行,相当于lines.split("\n") 123 lines = ''' 124 This is first line. 125 This is second line. 126 This is third line. 127 ''' 128 print(lines.splitlines()) 129 #输出结果:['', 'This is first line.', 'This is second line.', 'This is third line'] 130 131 print(16*"*") 132 print() 133 #大小写转换 134 print("aBcD".swapcase()) #AbCd 135 136 #转为标题,所有单词首字母大写 137 print("This is a title".title()) #This Is A Title