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'
相关推荐
吴佳浩5 小时前
Python入门指南(六) - 搭建你的第一个YOLO检测API
人工智能·后端·python
长安第一美人5 小时前
C 语言可变参数(...)实战:从 logger_print 到通用日志函数
c语言·开发语言·嵌入式硬件·日志·工业应用开发
Larry_Yanan6 小时前
Qt多进程(一)进程间通信概括
开发语言·c++·qt·学习
superman超哥6 小时前
仓颉语言中基本数据类型的深度剖析与工程实践
c语言·开发语言·python·算法·仓颉
不爱吃糖的程序媛6 小时前
Ascend C开发工具包(asc-devkit)技术解读
c语言·开发语言
bu_shuo6 小时前
MATLAB奔溃记录
开发语言·matlab
Learner__Q6 小时前
每天五分钟:滑动窗口-LeetCode高频题解析_day3
python·算法·leetcode
————A6 小时前
强化学习----->轨迹、回报、折扣因子和回合
人工智能·python
你的冰西瓜6 小时前
C++标准模板库(STL)全面解析
开发语言·c++·stl
徐先生 @_@|||7 小时前
(Wheel 格式) Python 的标准分发格式的生成规则规范
开发语言·python