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'
相关推荐
IVEN_7 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang9 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮9 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling9 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮12 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽12 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers