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删除序列的字符
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判定首部结束。
实验证明:所谓的首、尾,判定依据是-是否连续符合入参要求,如果符合,不论顺序,皆可操作,一直到遇到第一个非入参字符为止.
python strip() 函数探究的更多相关文章
- python strip()函数 介绍
python strip()函数 介绍,需要的朋友可以参考一下 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除 ...
- python strip()函数
转发:jihite-博客园-python strip()函数 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的 ...
- python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法
python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法 在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数 ...
- python strip() 函数和 split() 函数的详解及实例
strip是删除的意思:split则是分割的意思.strip可以删除字符串的某些字符,split则是根据规定的字符将字符串进行分割. 1.Python strip()函数 介绍 函数原型 声明:s为字 ...
- (转)python strip()函数 去空格\n\r\t函数的用法
原文:http://www.cnblogs.com/zdz8207/p/python_learn_note_20.html python3.4学习笔记(二十) python strip()函数 去空格 ...
- python strip()函数和Split函数的用法总结
strip函数原型 声明:s为字符串,rm为要删除的字符序列. 只能删除开头或是结尾的字符或是字符串.不能删除中间的字符或是字符串. s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除 ...
- python Strip函数和Split函数的用法总结 (python2.0,但用法与3.0是差不多的)
strip函数原型 声明:s为字符串,rm为要删除的字符序列. 只能删除开头或是结尾的字符或是字符串.不能删除中间的字符或是字符串. s.strip(rm) 删除s字符串中开头.结尾处, ...
- python strip()函数 os.popen()
函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字 ...
- Python strip()函数用法
Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数: strip 同时去掉左右两边的空格lstrip 去掉左边的空格rstrip 去掉右边的空格 具体示 ...
随机推荐
- IIS application pool access desktop denied
https://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permiss ...
- NSString字符串截取方法
1.字符串 1> 字符串比较 NSString *a = @“hello”; NSString *b = [NSString stringWithFormat:@hello”]; if (a = ...
- Ural2089:Experienced coach(二分图匹配)
Misha trains several ACM teams at the university. He is an experienced coach, and he does not undere ...
- java对象序列化的理解
1.java中的序列化时transient变量(这个关键字的作用就是告知JAVA我不可以被序列化)和静态变量不会被序列 化(下面是一个测试的例子) (实体带versionUUID,便 ...
- x86 linux 裁剪过程中能正常跑起来的必要配置项
A .选中Executable file formats/Emulations ---> Kernel support for ELFbinaries -----加载运行rootfs 中的程序. ...
- javascript 反斜杠\
通常,我们在动态给定一个div的innerHTML时,通常是样做的: <div id="demo1" /> <SCRIPT> var demo= docum ...
- .NET获取汉字首字母
/// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText" ...
- Linux 常用命令二 pwd cd
一.pwd命令 显示整个路径名: wang@wang:~$ pwd /home/wang 二.cd命令 切换到其他路径(相对路径方式): wang@wang:~$ cd workpalce/ wang ...
- bzoj 303: [CQOI2009]中位数图【前缀和+瞎搞】
处理出一个序列c,a[i]>b,c[i]=1;a[i]==b,c[i]=0;a[i]<b,c[i]=-1,然后s为c的前缀和,设w为b在a序列里的下标 注意到子序列一定横跨w,并且一个符合 ...
- UOJ #206. 【APIO2016】Gap【交互题】
参考:https://blog.csdn.net/clover_hxy/article/details/70767653 人生第一次交互题...不是很难但是思维和传统题差别挺大的(以及并不会本地测试= ...