Python数据结构知识(一)字符串 一、字符串定义Python 支持三种引号创建字符串单引号 和双引号 二者可以交替使用解决引号嵌套问题。三引号 / 支持换行书写长文本常用于多行字符串、文档字符串docstring。文档字符串又包含类注释和函数注释。# 字符串定义 str1 Hello World str2 Python Programming str3 He said, Hello! str4 多行 字符串二、转义与原始字符串常用转义字符\n换行\t制表符\、\输出普通引号\\输出反斜杠如路径C:\\Users原始字符串 r忽略所有转义常用于文件路径书写例如rC:\Users\Administrator。# 转义与原始字符串 print(换行\n制表\t反斜杠\\) print(引号\和\) path rC:\Users\Admin # 原始字符串 print(path)三、字符串格式化三种写法旧式占位格式化%s-%s % (a, b)format 格式化{}-{}.format(a, b)f-string思维导图未标出是常用扩展f{a}-{b}# 字符串格式化 name, age, score Alice, 25, 95.5 print(%s %d %.1f % (name, age, score)) # 旧式 print({} {} {:.1f}.format(name, age, score)) # format print(f{name} {age} {score:.1f}) # f-string四、索引与下标len()获取字符串总长度正向索引范围[0, len(s)-1]负值索引[-1, -len(s)]-1 代表最后一个字符# 索引与下标 text Python print(text[0], text[-1]) # P n print(len(text)) # 6五、字符串遍历for char in s直接遍历拿到每个字符for i in range(len(s))通过索引s[i]获取字符# 字符串遍历 text Python for ch in text: print(ch, end ) # P y t h o n print() for i in range(len(text)): print(text[i], end )六、切片 [start:stop:step]规则start包含stop不包含step步长默认 1[start:]从 start 截取到末尾[:stop]从开头截取到 stop-1[start:stop][start:stop:1][::-1]步长为负实现字符串反转# 切片操作 text Hello Python print(text[0:5]) # Hello print(text[6:]) # Python print(text[:5]) # Hello print(text[::-1]) # nohtyP olleH print(text[::2]) # HloPto七、字符串常用内置方法1. 查找类find()查找子串找不到返回 -1rfind()从右往左查找index()查找子串找不到直接报错rindex()从右往左查找count()统计子串出现次数# 查找类方法 text hello world hello print(text.find(world)) # 6 print(text.rfind(hello)) # 12 print(text.index(world)) # 6 print(text.count(hello)) # 22. 大小写转换upper()全部大写lower()全部小写title()每个单词首字母大写capitalize()整个字符串首字母大写swapcase()大小写互相转换# 大小写转换 text hello Python print(text.upper()) # HELLO PYTHON print(text.lower()) # hello python print(text.title()) # Hello Python print(text.capitalize()) # Hello python print(text.swapcase()) # HELLO pYTHON3. 对齐填充center()居中可自定义填充字符ljust()左对齐rjust()右对齐zfill()右对齐左侧自动填充 0# 对齐填充 text Python print(text.center(10, *)) # **Python** print(text.ljust(10, -)) # Python---- print(text.rjust(10, -)) # ----Python print(text.zfill(10)) # 0000Python4. 判断开头结尾startswith()判断是否以指定内容开头endswith()判断是否以指定内容结尾# 判断开头结尾 text hello.py print(text.startswith(hello)) # True print(text.endswith(.py)) # True print(text.endswith(.txt)) # False5. 去除首尾字符strip()去除首尾字符默认空格lstrip()/removeprefix()去除左侧前缀rstrip()/removesuffix()去除右侧后缀# 去除首尾字符 text hello print(text.strip()) # hello print(text.lstrip()) # hello print(text.rstrip()) # hello url https://example.com print(url.removeprefix(https://)) # example.com print(url.removesuffix(.com)) # https://example6. 拼接、切割、替换join(可迭代对象)使用当前字符串拼接多个元素split()按照指定字符切割字符串得到列表replace()字符串内容替换# 拼接、切割、替换 words [Hello, World] print(-.join(words)) # Hello-World text a,b,c,d print(text.split(,)) # [a, b, c, d] text I like Python print(text.replace(like, love)) # I love Python7. 编码与解码编码字符串.encode(utf-8) → 字节流 bytes解码字节流.decode(utf-8) → 字符串# 编码与解码 text 你好 encoded text.encode(utf-8) print(encoded) # b\xe4\xbd\xa0\xe5\xa5\xbd decoded encoded.decode(utf-8) print(decoded) # 你好8. 字符判断is 系列isalpha()是否全部字母isdigit()是否全部数字isalnum()是否由字母 数字组成# 字符判断 print(abc.isalpha()) # True print(123.isdigit()) # True print(abc123.isalnum()) # True print(abc 123.isalnum()) # False含空格9. format()字符串格式化# format() 格式化 name, age Alice, 25 print(Name: {}, Age: {}.format(name, age)) print(Name: {0}, Age: {1}.format(name, age)) print(Name: {n}, Age: {a}.format(nname, aage))八、补充重要特性字符串属于不可变类型所有方法都不会修改原字符串只会返回新字符串。# 字符串不可变性验证 text Hello # text[0] h # 报错字符串不可修改 text text.replace(H, h) # 必须重新赋值 print(text) # hello