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() 函数探究的更多相关文章

  1. python strip()函数 介绍

    python strip()函数 介绍,需要的朋友可以参考一下   函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 rm删除 ...

  2. python strip()函数

    转发:jihite-博客园-python strip()函数 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 rm删除序列的 ...

  3. python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法

    python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法 在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数 ...

  4. python strip() 函数和 split() 函数的详解及实例

    strip是删除的意思:split则是分割的意思.strip可以删除字符串的某些字符,split则是根据规定的字符将字符串进行分割. 1.Python strip()函数 介绍 函数原型 声明:s为字 ...

  5. (转)python strip()函数 去空格\n\r\t函数的用法

    原文:http://www.cnblogs.com/zdz8207/p/python_learn_note_20.html python3.4学习笔记(二十) python strip()函数 去空格 ...

  6. python strip()函数和Split函数的用法总结

    strip函数原型 声明:s为字符串,rm为要删除的字符序列. 只能删除开头或是结尾的字符或是字符串.不能删除中间的字符或是字符串. s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除 ...

  7. python Strip函数和Split函数的用法总结 (python2.0,但用法与3.0是差不多的)

    strip函数原型 声明:s为字符串,rm为要删除的字符序列. 只能删除开头或是结尾的字符或是字符串.不能删除中间的字符或是字符串. s.strip(rm)        删除s字符串中开头.结尾处, ...

  8. python strip()函数 os.popen()

    函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字 ...

  9. Python strip()函数用法

    Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数: strip 同时去掉左右两边的空格lstrip 去掉左边的空格rstrip 去掉右边的空格 具体示 ...

随机推荐

  1. Koa2学习(四)POST请求

    Koa2学习(四)POST请求 接受请求 POST请求的数据实体,会根据数据量的大小进行分包传送. 当node.js后台收到post请求时,会以buffer的形式将数据缓存起来.Koa2中通过ctx. ...

  2. Mac配置环境变量(Java,Android,Gradle,Nodejs,MongoDB,Maven,Hosts)

    JAVA_HOME 配置环境变量 # 使用vim打开.bash_profile文件.加入java环境变量 $ vim .bash_profile export JAVA_HOME=$(/usr/lib ...

  3. hexSHA1散列加密解密(不可逆)

    1.maven引入codec和commons依赖: <dependency> <groupId>commons-codec</groupId> <artifa ...

  4. 并不对劲的[USACO07NOV,洛谷p2886]Cow Relays

    题意就是给一张无向有边权的图.起点.终点,求起点到终点经过n条边的最短路.n<=10^6,点的编号<=10^3,边数<=10^2. 这个边数让人不由自主地想到了floyd,然后发现f ...

  5. BZOJ_3881_[Coci2015]Divljak_AC自动机+dfs序+树状数组

    BZOJ_3881_[Coci2015]Divljak_AC自动机+dfs序+树状数组 Description Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是 ...

  6. 【Usaco2008 Dec】Patting Heads

    [题目链接] 点击打开链接 [算法] 我们知道,每个编号为a[i]都要被编号是a[i]的约数的牛拍一次头(除了它自己),因此,只需用类似于筛法的方式统计答案, 即可 [代码] #include< ...

  7. python 操作memercache类库

    pip install python-memcached pip install  pymemcache pip install   python-libmemcached

  8. 洛谷 P1081 开车旅行【双向链表+倍增】

    倍增数组的20和N写反了反复WAWAWA-- 注意到a和b在每个点上出发都会到一个指定的点,所以这样构成了两棵以n点为根的树 假设我们建出了这两棵树,对于第一问就可以枚举起点然后倍增的找出ab路径长度 ...

  9. 【OpenJ_Bailian - 2192】Zipper(dfs)

    Zipper Descriptions: Given three strings, you are to determine whether the third string can be forme ...

  10. mqtt遇到的问题锦集

    1.无效客户机标识 (2) Connect指令中的KeepAlive有效范围[60秒,300秒],否则会拒绝连接. 2.消息回调出现频繁的断开连接 待解决 3.长时间消息回调出现 已断开连接 (321 ...