python strip() 函数探究
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删除序列的字符
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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'
究竟何为’首尾’?实验之
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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判定首部结束。
实验证明:
所谓的首、尾,判定依据是-是否连续符合入参要求,如果符合,不论顺序,皆可操作,一直到遇到第一个非入参字符为止.
python strip() 函数探究相关推荐
- python strip() 函数探究
strip()方法语法:str.strip([chars]); 声明:str为字符串,rm为要删除的字符序列 str.strip(rm) 删除字符串中开头.结尾处,位于rm删除序列的字符 eg1: # ...
- python strip()函数和Split函数的用法总结
目录 1.python中for循环如何控制步长 2.python strip()函数和Split函数的用法总结 1.python中for循环如何控制步长 for i in range(开始/左边界, ...
- python中用什么函数去掉空格_python去除字符串空格-python删除空格-python去掉空白字符-python strip函数-python trim函数-嗨客网...
Python去除字符串空格教程 在开发过程中,有时候我们需要去除一个 Python strip()函数详解 语法 S.strip([chars]) -> str 参数 参数 描述 S 表示原字符 ...
- python去空格的函数_(转)python strip()函数 去空格\n\r\t函数的用法
原文:http://www.cnblogs.com/zdz8207/p/python_learn_note_20.html python3.4学习笔记(二十) python strip()函数 去空格 ...
- python strip函数用法_python strip()函数 介绍
描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除字符串头尾指定 ...
- python strip函数用法_python中strip函数的用法
python中往往使用剥除函数strip()来对用户的输入进行清理.strip函数的最一般形式为: str.strip('序列') 其中,序列是一段字符串,该函数表示从头或者从尾部开始进行扫描,如果扫 ...
- python strip 函数用法及介绍
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. Python 3.6.5 (defa ...
- python中strip函数_让你整明白python strip函数妙用
以下为作者学习python时所做的学习笔记,用浅显的语言展示给大家,希望大家多多提交自己的意见.有问题的朋友也可以留言,看到会回复大家. 声明:s为字符串,str为要删除的字符序列 s.strip(s ...
- python爬虫表格中清除空格_爬虫清洗:python strip()函数 去空格\n\r\t函数的用法
在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数: strip 同时去掉左右两边的空格 lstrip 去掉左边的空格 rstrip 去掉右边的空格 ...
最新文章
- win10 调用计算机,Win10系统打开此电脑提示正在处理它解决方法
- 自然语言处理 的 一些环境和包
- canvas 中的元素拖拽
- Python3打印当前系统时间
- React-navigation之StackNavigator
- 暑期读书月开幕式暨天一寻宝活动
- Mac读写NTFS移动硬盘
- HugePages 大内存页
- 2019产品数据管理(PDM)技术说明
- HandlerInterceptor的使用
- 对keep-alive的理解是什么?
- Datawhale学习笔记【阿里云天池 金融风控-贷款违约预测】Task2 数据分析
- Java统计List中每个元素出现的次数、用java实现生成或显示文件的一些数字、微信小程序开发回顾
- ##MYfirstLog
- JQUERY为元素添加样式的实现方法
- UART中的硬件流控RTS与CTS
- 【Unity3D插件】EasyAR插件分享《AR使用及开发案例》
- 数据分析——用户消费行为分析
- 【比赛报告】2018.10.15校赛[2015-9-13 NOIP模拟赛 by hzwer] NOIP练习赛卷十四
- ATP250 Bway体育梅兹站1/4决赛 瓦林卡5连胜 杀入了4强名列