Python之字符串分割替换移除

Python之字符串分割替换移除

分割

  • split(sep=None, maxsplit=-1) -> list of strings
    • 从左至右
    • sep 指定分割字符串,缺省的情况下空白字符串作为分隔符
    • maxsplit 指定分割的次数,-1 表示遍历整个字符串
    • 立即返回列表
  • rsplit(sep=None, maxsplit=-1) -> list of strings
    • 从右向左开始切,但是输出的字符串字符不会反
    • sep 指定分割字符串,缺省的情况下空白字符串作为分隔符
    • maxsplit 指定分割的次数,-1 表示遍历整个字符串
    • 立即返回列表
  • splitlines(keepends) -> list of strings
    • 按照行来切分字符串
    • keepends 指的是是否保留行分隔符
    • 行分隔符包括\n、\r\n、\r等
python 复制代码
str.split?
# str.split(self, /, sep=None, maxsplit=-1)
# 查看split的帮助信息。
python 复制代码
",".join('abc')
# 使用join方法拼出一个新的字符串
# 返回结果:'a,b,c'
python 复制代码
a = ",".join('abc').split('b')
print(a)
# 指定b为分割符
# 返回结果:['a,', ',c'],b被切掉了。
  • x等于一个列表,列表中有3个str类型的值,y等于x,使用冒号作为拼接字符
python 复制代码
y.split(':')
# y的值用split的冒号进行分割
# 返回结果:['a', 'b', 'c']
python 复制代码
y.split(':', 1)
# 1是分割一次,如果不指定默认值是-1,从头切到尾
# 返回结果:['a', 'b:c']
python 复制代码
":a:b:c:".split(':')
# 返回结果:['', 'a', 'b', 'c', '']
# 切掉的部分补空格。
python 复制代码
str.rsplit?
# str.rsplit(self, /, sep=None, maxsplit=-1)
# rsplit的帮助信息。
python 复制代码
r"c:\windows\nt".rsplit('\\',1) 
# 从右分割一次,使用\\是因为1个\会被转义。
# 返回结果:['c:\\windows', 'nt']
python 复制代码
'a\nb\r\nc\rd\te   \n  \t  f'.split()
# 不指定分割符,按照空白字符切
# 返回结果:['a', 'b', 'c', 'd', 'e', 'f']
python 复制代码
'a\nb\r\nc\rd\te   \n  \t  f'.split('\n')
# 如果指定了分隔符,指定了什么就使用什么进行分割
# 返回结果:['a', 'b\r', 'c\rd\te   ', '  \t  f']
python 复制代码
'a\nb\r\nc\rd\te   \n  \t  f'.splitlines()
# 只分割 \r \n \r\n
# 返回结果:['a', 'b', 'c', 'd\te   ', '  \t  f']
python 复制代码
'a\nb\r\nc\rd\te   \n  \t  f'.splitlines(True)
# 切掉的分隔符又回来了
# 返回结果:['a\n', 'b\r\n', 'c\r', 'd\te   \n', '  \t  f']
python 复制代码
'a\nb\r\nc\rd\te   \n  \t  f'.splitlines(False)
# 返回结果:['a', 'b', 'c', 'd\te   ', '  \t  f']
python 复制代码
"abcd".partition('b')
# 指定b为分割符,只切一刀,返回元组
# 返回结果:('a', 'b', 'cdb')
python 复制代码
"a,b,c,d".split(':')
# 如果指定参数不能分割,返回一个列表,列表中是原值
# 返回结果:['a,b,c,d']
python 复制代码
"a,b,c,d".partition(":")
# 如果指定参数不能分割就返回空串
# 返回结果:('a,b,c,d', '', '')
python 复制代码
"a,b,c,d".rsplit(':')
# 返回结果:['a,b,c,d']
python 复制代码
"a,b,c,d".rpartition(":")
# 返回结果:('', '', 'a,b,c,d')

替换

  • replace(old, new, count) -> str
    • 字符串中找到匹配替换为新子串,返回新字符串
    • count表示替换几次,不指定就是全部替换
python 复制代码
str.replace?
# str.replace(self, old, new, count=-1, /)
python 复制代码
"a,b,c".replace(',', ' * ') 
# 括号中指定,前面是替换的内容逗号,后面是替换的值星号
# 返回结果:'a * b * c'
python 复制代码
"www.simple.com".replace('w', 'a')
# 返回结果:'aaa.simple.com'
python 复制代码
"www.simple.com".replace('ww', 'a')
# 返回结果:'aw.simple.com'
python 复制代码
"www.simple.com".replace('www', 'a')
# 返回结果:'a.simple.com'
python 复制代码
"www.simple.com".replace('ww', 'w')
# 返回结果:'ww.simple.com'

移除

  • strip(chars) -> str
    • 在字符串两端去除指定的字符集chars中的所有字符
    • 如果chars没有指定,去除两端的空白字符
  • lstrip(chars) -> str ,从左开始
  • rstrip(chars) -> str,从右开始
python 复制代码
str.strip? # 移除
# str.strip(self, chars=None, /)
str.rstrip? # 右移除
# str.rstrip(self, chars=None, /)
str.lstrip? # 左移除
# str.lstrip(self, chars=None, /)
python 复制代码
'\t\ra,b '.strip()
# 空白字符全移除
# 返回结果:'a,b'
python 复制代码
'\t\ra,b '.rstrip()
# 空白字符右移除
# 返回结果:'\t\ra,b'
python 复制代码
'\t\ra,b '.lstrip()
# 空白字符左移除
# 返回结果:'a,b '
python 复制代码
'\r\n\t    a\nb\r\nc\rd\te    \n    \t   f\f\t    '.strip()
# strip 将字符串两端的空白字符拿掉
# 返回结果:'a\nb\r\nc\rd\te    \n    \t   f'
python 复制代码
'\r\n\t    a\nb\r\nc\rd\te    \n    \t   f\f\t    '.strip('\r\t')
# 返回结果:'\n\t    a\nb\r\nc\rd\te    \n    \t   f\x0c\t    '
python 复制代码
'\r\n\t    a\nb\r\nc\rd\te    \n    \t   f\f\t    '.strip('\r\t ')
# 返回结果:'\n\t    a\nb\r\nc\rd\te    \n    \t   f\x0c'
python 复制代码
'\r\n\t    a\nb\r\nc\rd\te    \n    \t   f\f\t    '.strip('\r\t \n')
# 返回结果:'a\nb\r\nc\rd\te    \n    \t   f\x0c'
python 复制代码
'\r\n\t    a\nb\r\nc\rd\te    \n    \t   f\f\t    '.strip('\r\t \n\fabf')
# 返回结果:'c\rd\te'
相关推荐
SeanQhl7 小时前
用 Python 给长文做排版体检:图够不够、段落是不是太长
python·自动化·markdown·代码规范·写作工具
SeanQhl8 小时前
中文 Windows 上跑 Python 定时任务——编码、路径、控制台三个坑
windows·python·编码·效率工具·定时任务
apcipot_rain9 小时前
C++开发 面试 1Day速通
开发语言·c++·面试
时间的情敌9 小时前
Three.js GLTF 阀门 / 泵体模型加载、材质修改、状态联动(正常 / 故障 / 告警变色)
开发语言·javascript·three.js·材质
星恒随风9 小时前
从0实现C++ AI大模型接入SDK(七):Ollama本地模型部署、NDJSON流式响应与LLMManager模型管理
linux·开发语言·c++·人工智能·笔记
积流成海10 小时前
Agent演化史(十):2027年它会消失吗?(终章)
python·agent·智能体·大模型应用
琳少_10 小时前
java课堂笔记
java·开发语言·数据库·笔记
专注仿真11 小时前
仿真智能体训练:强化学习中的奖励塑形算法与实战
python·强化学习·afsim智能体·仿真训练·afsim智能体训练
30955666612 小时前
股票接口日期参数为什么不起作用:五组请求对照与逐字节比对排查笔记
服务器·数据库·笔记·python·量化·api接口·股票接口
Brain 博览记12 小时前
脑机接口研究丨FPGA 5 毫秒完成运动想象分类
开发语言·论文阅读·人工智能·机器学习·fpga开发·分类·数据挖掘