1 ''' 2 1. 在Python中,str是内置类,代表字符串(即文本数据)。字符串是Python中最常用的数据类型之一,它允许您存储和操作文本数据。 3 2. 字符串类str是不可变类。即不管怎么操作,原字符串还是存在的,像replace、大小写转换等操作只是创建一个新的字符串 4 5 官网参考资料: 6 https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str 7 ''' 8 9 # 1. 通过内置函数help查看内置类str的docstring 10 # help(str) 11 12 # 2. 字符串字面值: 您可以使用单引号 '、双引号 " 或三引号 ''' 或 """ 来创建字符串字面值。 13 single_quoted_string = 'Hello, World!' # 单引号 14 double_quoted_string = "Hello, World!" # 双引号 15 triple_quoted_string = '''Hello, 16 World!''' # 3个单引号,支持换行,而不用添加\n。即所见即所得,并且注释只能放在最后代码的,不能放在14行 17 18 ''' 19 输出: 20 Hello, 21 World! 22 ''' 23 print(triple_quoted_string) 24 25 # 3. 转义字符: 字符串中的转义字符以反斜杠 \ 开头,用于表示特殊字符或字符序列。 26 my_string = "Hello, \"World!\"" 27 # 如果外面使用单引号,内部就可以直接使用双引号了。同理外面使用双引号,内部就可以直接使用单引号。否则需要转义处理 28 my_string = 'Hello, "World!"' 29 print(my_string) # 输出: Hello, "World!" 30 31 # 4. 字符串索引: 字符串中的每个字符都有一个索引,索引从0开始,可以使用方括号 [] 来访问特定索引位置的字符。 32 my_string = "Hello, World!" 33 print(my_string[0]) # 输出: "H" 34 print(my_string[7]) # 输出: "W" 35 36 # 5. 切片操作: 使用切片操作可以获取字符串的子串,语法为 [start:end:step],不包括end 37 my_string = "Hello, World!" 38 print(my_string[0:5]) # 输出: Hello 39 print(my_string[7:]) # 输出: World! 40 print(my_string[::-1]) # 输出: !dlroW ,olleH(字符串反转) 41 print(my_string[-3:-1]) # 输出: ld 42 43 # 6. 字符串的拼接和拆分 44 45 # 6.1 字符串拼接: 可以使用 + 运算符或字符串的 join() 方法来拼接字符串 46 first_name = "John" 47 last_name = "Doe" 48 full_name = first_name + " " + last_name 49 print(full_name) # 输出: "John Doe" 50 51 words = ["Hello", "World", "!"] 52 sentence = "_".join(words) # "_"为指定链接的链接符,可根据需求自行定义 53 print(sentence) # 输出: "Hello_World_!" 54 55 # 6.2 拆分: 56 my_string = "Hello,Wo,rl,d!" 57 words = my_string.split(",") 58 print(words) # 输出: ['Hello', 'Wo', 'rl', 'd!'] 59 60 words = my_string.split(",", 1) # 如果存在拆分字符,则会生成 n + 1个元素的列表即此例2个元素的列表 61 print(words) # 输出: ['Hello', 'Wo,rl,d!'] 62 63 my_string = "Allen" 64 words = my_string.split(",", 1) # 输出: ['Allen'],不存在拆分字符,则返回一个1元素的列表。 65 print(words) # 输出: ['Allen'] 66 67 # 7. 字符串格式化: 使用字符串的 format() 方法或 f-string(格式化字符串字面值)进行字符串的格式化。 68 name = "XX" 69 age = 25 70 message = "My name is {} and I'm {} years old.".format(name, age) 71 print(message) # 输出: "My name is XX and I'm 25 years old." 72 73 name = "Allen" 74 age = 30 75 message = f"My name is {name} and I'm {age} years old." 76 print(message) # 输出: "My name is Allen and I'm 30 years old." 77 78 # 8. 字符串的检查和验证:使用字符串的相关方法进行检查和验证,例如 isdigit()、isalpha()、islower()、isupper() 等。 79 80 # 8.1 判断是否是数字型字符串 81 num = "123" 82 print(num.isdigit()) # 输出: True 83 num = "123.456" 84 print(num.isdigit()) # 输出: Talse,因为有个. 85 86 # 8.2 是否是纯字母 87 name = "Alice" 88 print(name.isalpha()) # 输出: True 89 90 # 8.3 是否是数字、字母组成,无其他字符 91 name = "Allen7788XX" 92 print(name.isalnum()) # 输出: True 93 94 # 8.4 判断字母是不是全部是小写.可以包含非字母字符 95 my_string = "hello" 96 print(my_string.islower()) # 输出: True 97 98 my_string = "hello88!" 99 print(my_string.islower()) # 输出: True 100 101 my_string = "hello88A" 102 print(my_string.islower()) # 输出: Talse 103 # isupper方法判断是否是纯大写字母 104 print(my_string.isupper()) # 输出: Talse 105 106 # 8.5 判断字符串以什么开头或结尾,支持指定区间判断 107 my_string = "Hello, World!" 108 print(my_string.startswith("Hello")) # 输出: True 109 110 my_string = "Hello, World!" 111 print(my_string.endswith("World!")) # 输出: True 112 113 my_string = "Hello, World!" 114 print(my_string.endswith("World!", 0, 7)) # 输出: False 115 116 # isspace():如果字符串中只包含空⽩,则返回 True,否则返回 False 117 print(my_string.isspace()) 118 # 用于检查字符串是否只包含十进制数字字符。它返回一个布尔值,如果字符串只包含十进制数字字符,则返回 True,否则返回 False。 119 my_string = "A18b" 120 print(my_string.isdecimal()) # 输出: False 121 122 my_string = "18" 123 print(my_string.isdecimal()) # 输出: True 124 age = input("请输入年龄:") 125 if age.isdecimal(): 126 age = int(age) 127 if age >= 18: 128 print("你是一个成年人") 129 else: 130 print("你还未成年") 131 else: 132 print("请输入一个有效的数字") 133 134 # 9. str():将其他数据类型转换为字符串类型。 135 num = 10 136 string_num = str(num) # 将整型转为字符串类型 137 print(string_num) # 输出: "10" 138 139 # 10. 计算:字符字符串的长度,计算子串的个数 140 141 # 10.1 len(): 返回字符串的长度。 142 my_string = "Hello, World!" 143 print(len(my_string)) # 输出: 13 144 145 # 10.2 146 147 child_string = "Allen" 148 parent_string = "Allen123Allen456All" 149 print(parent_string.count(child_string)) # 输出: 2 150 151 # 11. 字母转换: 字母全部转换为大写或小写、字符大小写转换、字符串的首字母大写,其他字符小写。 152 # 原字符串不变,新生成一个新的字符串 153 print(child_string.lower()) # 输出: allen 154 print(child_string.upper()) # 输出: ALLEN 155 child_string = "allenhao" 156 print(child_string.upper()) # 输出: ALLEN_HAO 157 child_string = "allen_hao" 158 159 print(child_string.capitalize()) # 输出: Allen_hao 160 child_string = "allen hao" 161 print(child_string.capitalize()) # 输出: Allen hao即第1个字符是字母的就处理为大写 162 child_string = "8allen hao" 163 print(child_string.capitalize()) # 输出: 8allen hao即第1个字符是字母的就处理为大写,否则就不处理 164 165 # 大写变小写,小写变大写 166 name = "AllEn" 167 print(name.swapcase()) # aLLeN 168 169 # 12. replace(old, new):将字符串中的旧子字符串替换为新的子字符串。 170 my_string = "Hello, World!" 171 new_string = my_string.replace("Hello", "Hi") 172 print(new_string) # 输出: "Hi, World!" 173 174 175 # 13. 去除前后空格: lstrip、rstrip、strip 176 # 1. 这些方法只删除字符串开头和结尾的空格,不会删除字符串中间的空格。 177 # 2. 这些方法只删除空格字符,不会删除其他的空白字符,如制表符和换行符。如果需要删除其他空白字符,可以使用 replace() 方法。 178 # 3. 注意这些方法返回一个新的字符串,而不会修改原始字符串。如果需要修改原始字符串,可以将结果赋值给原始变量。 179 # 4. 这些方法只能删除字符串开头和结尾的空格,如果需要删除其他字符,可以考虑使用 replace() 方法或使用正则表达式来替换 180 181 # 13.1 去除字符串头部空格即字符串开头是空才去除,不管几个空格,都去除。否则啥也不处理 182 ''' 183 输出结果: 184 Hello, World! 185 Hello, World! 186 Hello, World! 187 ''' 188 s = " Hello, World! " 189 print(s.lstrip()) # 输出 "Hello, World! " 190 191 # 13.2 去除字符串结尾出空格即字符串结尾是空才去除,不管几个空格,都去除。否则啥也不处理 192 s = " Hello, World! " 193 print(s.rstrip()) # 输出 " Hello, World!" 194 195 # 13.3 等价于lstrip + rstrip 196 s = " Hello, World! " 197 print(s.strip()) # 输出 "Hello, World!" 198 199 s = "\t Hello, World! " 200 print(s.strip()) # 输出 Hello, World!,\t处理了 201 print(s) # 输出 Hello, World! 202 203 s = '''Hello, 204 World!''' 205 print(s.strip()) # 输出 换行没有处理 206 ''' 207 Hello, 208 World! 209 ''' 210 211 # 14. 对齐或说字符串进行填充操作 212 # ljust(width, fillchar) 方法在原始字符串的右侧填充字符,使得新字符串总长度为指定的 width。 213 # 如果所需填充的长度小于 width,则可以使用指定的 fillchar 进行填充。 214 # 如果所需填充的长度大于 width,则直接显示字符串即不需要填充,如果用于对齐,一定要算出width的值。 215 s = "Hello" 216 print(s.ljust(10, "*")) # 输出 "Hello*****" 217 print(s.ljust(3, "*")) # 输出 "Hello" 218 219 # rjust(width, fillchar) 方法在原始字符串的左侧填充字符,使得新字符串总长度为指定的 width 220 s = "Hello" 221 print(s.rjust(10, "*")) # 输出 "*****Hello" 222 print(s.rjust(3, "*")) # 输出 "Hello" 223 # center(width, fillchar) 方法在原始字符串的两侧填充字符,使得新字符串总长度为指定的 width。 224 s = "Hello" 225 print(s.center(10, "*")) # 输出 "**Hello***" 226 print(s.center(3, "*")) # 输出 "Hello"