strip()方法语法:str.strip([chars]);

声明:str为字符串,rm为要删除的字符序列

  • str.strip(rm) 删除字符串中开头、结尾处,位于rm删除序列的字符
eg1:
#首尾端'0'被删除,中间不动
>>> t='0000this is string example0000wow!!!0000'
>>> t.strip('0')
'this is string example0000wow!!!' eg2:
#无入参,默认删除首尾所有空字符 ‘\n\t空格‘
>>>s='\n 0000this is string example0000wow!!!0000\n \t'
>>> s.strip()
'0000this is string example0000wow!!!0000'
  • str.lstrip(rm) 删除字符串中开头处,位于 rm删除序列的字符
t='0000this is string example0000wow!!!0000'
>>> t.lstrip('0')
'this is string example0000wow!!!0000' #空入参同样可删除首部空字符,'.rstrip()'同理
s='\n 0000this is string example0000wow!!!0000\n \t'
>>> s.lstrip()
'0000this is string example0000wow!!!0000\n \t'
  • str.rstrip(rm) 删除字符串中结尾处,位于 rm删除序列的字符
t='0000this is string example0000wow!!!0000'
>>> t.rstrip('0')
'0000this is string example0000wow!!!' s='\n 0000this is string example0000wow!!!0000\n \t'
>>> s.rstrip()
'\n 0000this is string example0000wow!!!0000'

究竟何为'首尾'?实验之

s='\n 0000this is string is example0000wow!!!0000\n \t'
>>> s.lstrip('\n 0')
'this is string is example0000wow!!!0000\n \t'
#首部'\n 0000'被删除 >>> s.lstrip('\n 0this')
'ring is example0000wow!!!0000\n \t'
#奇妙啊,我的目标是删除首部'\n 0000this',结果'\n 0000this is st'全被删除,说明:符合入参('\n 0this')的字符皆是删除对象,不论字符顺序
#但,为何string后面的is没有删除?因为,'首部'指的是'连续符合'入参要求的字符,string中的'r'隔断了入参的连续字符要求,python判定首部结束。

实验证明:所谓的首、尾,判定依据是-是否连续符合入参要求,如果符合,不论顺序,皆可操作,一直到遇到第一个非入参字符为止.

最新文章

  1. mysql5.7.13-windows 免安装版配置简介
  2. 转: Eclipse自动提示功能
  3. BZOJ4527: K-D-Sequence 线段树
  4. UIScrollView 原理详解
  5. Maven的安装、配置及使用入门
  6. Binary Tree Paths
  7. jquery ajax 开发手记
  8. NSMutableParagraphStyle /NSParagraphStyle
  9. Python httpsqs封装类
  10. alias 命令详解
  11. Android的Ui层次
  12. 【憩园】C#并发编程之异步编程(二)
  13. java操作mongodb & springboot整合mongodb
  14. Antd Select组件结合使用出现must set key for <rc-animate> children问题
  15. IDEA搭建本地服务器解决无法连接https://start.spring.io
  16. Spring Boot系列——Spring Boot如何启动
  17. 卡方分布 | t检验 | F检验 | 卡方检验 | 假设检验 | 各种检验持续总结
  18. 相同类型的对象不能互相转换:java.lang.ClassCastException: com.anhoo.po.UserPo cannot be cast to com.anhoo.po.UserPo
  19. bzoj 1212: [HNOI2004]L语言
  20. 淘宝cnpm(可替代nodejs默认npm)

热门文章

  1. vim记住上次编辑和浏览位置
  2. 以太坊 EVM内交易执行分析(一)
  3. spring各个jar作用
  4. 并不对劲的p3676:小清新数据结构题
  5. oracle分区表有什么作用
  6. TCP 拆、粘包
  7. 关于MYSQL编辑乱码问题
  8. bzoj 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头【瞎搞】
  9. How to Compare Means (均值比较)
  10. Luogu P1396 营救【最小生成树/二分答案/最短路】 By celur925