一篇足够!python字符串索引、遍历、切片、各种字符串操作 详解

一篇足够!python字符串索引、遍历、切片、各种字符串操作 详解 本文涉及到了python中各种 字符串的操作流程以便于为python初学者提供学习目录一、字符串索引与遍历1.索引2.遍历二、字符串切片语法核心标准格式分类案例演示三.字符串的操作3.1查找3.2转换大小写3.3对齐方式3.4开始、结尾3.5剔除指定字符 默认剔除空白3.6切割与拼接与替换3.7编码、解码3.8 is*类型一、字符串索引与遍历1.索引1. 1正向索引从左向右下标从 0 开始字符串中每一个字符都有专属编号最左边第一个字符下标是0依次递增。python运行text Python # 正向取值 print(text[0]) # P print(text[1]) # y print(text[5]) # n1.2. 反向索引从右向左下标从 - 1 开始从末尾反向取值-1代表最后一个字符-2倒数第二个方便取末尾字符不用计算长度。python运行text Python print(text[-1]) # n 最后一位 print(text[-2]) # o 倒数第二位 print(text[-6]) # P 第一位1.3. 索引取值注意事项下标超出字符长度会直接报错IndexError单个索引只能获取1 个字符想要一段文字必须用切片2.遍历以下面例子为例msg abcdefghijk print(len(msg)) # length print(msg[0], msg[9])2.1遍历方式1for e in msg: print(e)2.2遍历方式2先通过range结合len 拿到每一个字符对应的索引for i in range(len(msg)): print(i, msg[i])二、字符串切片语法核心标准格式字符串[start : end : step]start起始下标包含该字符省略默认 0end结束下标不包含该字符省略默认取到末尾step步长每隔几个字符取一个正数从左往右负数从右往左省略默认 1分类案例演示1. 基础切片 [start:end]python运行s abcdefghijk # 取下标2到下标5不含5 print(s[2:5]) # cde # 从开头切到下标4 print(s[:4]) # abcd # 从下标3切到末尾 print(s[3:]) # defghijk # 完整复制整个字符串 print(s[:]) # abcdefghijk2. 带步长切片 [start:end:step]python运行s 0123456789 # 隔1个取1个取偶数位 print(s[::2]) # 02468 # 隔2个取1个 print(s[1::3]) # 1473. 反向切片步长为负数python运行s HelloWorld # 字符串整体反转最常用 print(s[::-1]) # dlroWolleH # 从倒数第2位反向截取 print(s[-2:-6:-1]) # lroW三.字符串的操作3.1查找index: 查找左边第一个对应的一个字符返回子字符串找不到字符串会报错print(hello world 123.index(e))rindex :从右侧开始找print(hello world 123.rindex(e))find: 开始查找 字符 起始位 停止位 若未标识默认全部查询print(hello world 123.find(9)) print(hello world 123.find(1,2, 6))rfind: 从右侧开始找print(hello world 123.find(3)) print(hello world 123.rfind(1,2, 6))count: 统计子字符串出现的次数 没有出现返回0# print(hello world 123.count(l)) # 也可以标注 起始位 停止位 默认整个字符串 # print(hello world 123.count(l, 5, 7))start, end 查找区间 默认整个字符串3.2转换大小写lower: 字母小写转换print(Hello world 123.lower())upper: 字母大写转换print(Hello world 123.upper())title: 将单词首字母大写print(Hello world 123.title())capitalize: 将字符串首字母大写print(Hello world 123.capitalize())swapcase: 将字符串中 大小写调换print(Hello world 123.swapcase())3.3对齐方式center居中,可以选择填充 内容print(hello world 123.center(20, *))ljust左对齐 可以选择填充 内容print(hello world 123.ljust(20, *))rjust: 右对齐 可以选择填充 内容print(hello world 123.rjust(20, *))zfill: 补0对齐 默认补0对齐 不能改变print(hello world 123.zfill(20))3.4开始、结尾startswith:开始结尾 查找选定字符串 是否在开始左边print(hello world 123.startswith( 123 ))endswith:开始结尾 查找选定字符串 是否在结尾右边print(hello world 123.endswith( 123))3.5剔除指定字符 默认剔除空白strip: 剔除空白 默认print( hello world 123 .strip())lstrip:剔除选定内容print(hello world 123.strip( )lstrip:剔除左边 选取内容print(hello world 123 .lstrip( ))rstrip:剔除右边 选取内容print(hello world 123.rstrip( ))3.6切割与拼接与替换split切割 默认在字符串里 以空格为分隔符 划分print(hello world 123.split()) #可以选定 内容 作为分隔符 print(hello world 123.split(1))join:拼接 将选定内容 加入到指定字符串中print( .join(helloworld123)) print( .join([hello, world]))replace: 替换将 字符串里的 指定 内容 替换为 相应内容 并指明替换的次数从左往右数 有几个替换的内容 按照替换次数进行替换print(hello hello hello 123.replace(hello, h i , 1)) print(hello hello hello 123.replace(hello, h i , 2)) print(hello hello hello 123.replace(hello, h i , 3))3.7编码、解码.encode(utf8) 返回utf8编码的字节流: 将一个字符串编译为 utf8 编码print(hello world 123 好困呀.encode(utf8)b.decode(utf8) 将utf8编码的字节流 解码成utf8字符串: 将 utf8 编码 b 转为 原来的字符串print(bhello world 123 \xe5\xa5\xbd\xe5\x9b\xb0\xe5\x91\x80.decode(utf8))3.8 is*类型is*: 判断一个字符串是不是 后面的函数类型print(az.isalpha()) print(19.isdigit()) print(19az.isalnum()) print(Az.islower()) print(Az.isupper()) print(Az Bz.istitle())希望以上笔者的文章对你有帮助