Python中strip方法的妙用
【开胃小菜】
当提到python中strip方法,想必凡接触过python的同行都知道它主要用来切除空格。有下面两种方法来实现。
方法一:用内置函数
#<python>
if __name__ == '__main__':
str = ' Hello world '
print '[%s]' %str.strip()
#</python>
方法二:调用string模块中方法
#<python>
import string
if __name__ == '__main__':
str = ' Hello world '
print '[%s]' %string.strip(str)
#</python>
不知道大家是否知道这两种调用有什么差别?下面是个人一些看法
Ø str.strip()是调用python的内置函数,string.strip(str)是调用string模块中的方法
Ø string.strip(str)是在string模块定义的。而str.strip()是在builtins模块中定义的
问题一: 怎样查看一个模块中方法是否在内置模块有定义?
用dir(模块名)看是否有'__builtins__'属性。
比如:查看string模块
#<python> print dir(string)
#</python>
问题二、怎样查看python中全部的内置函数
#<python>
print dir(sys.modules['__builtin__'])
#</python>
问题三、怎样查看内置模块中内置函数定义
#<python> print help(__builtins__)
#</python>
以上一些都是大家平时都知道的,接下来就进入本文的主题:
【饭中硬菜】
首先请大家看一下下列程序的执行结果:
#<python>
if __name__ == '__main__':
str = 'hello world'
print str.strip('hello')
print str.strip('hello').strip()
print str.strip('heldo').strip()
#sentence 1
stt = 'h1h1h2h3h4h'
print stt.strip('h1')
#sentence 2
s ='123459947855aaaadgat134f8sfewewrf7787789879879'
print s.strip('0123456789')
#sentence 3
#</python>
结果见下页:
执行结果:
world
world
wor
2h3h4
aaaadgat134f8sfewewrf
你答对了吗?O(∩_∩)O~
假设你都答对了,在此处我奉上32个赞 …
结果分析:
首先我们查看一下string模块中的strip源代码:
#<python>
# Strip leading and trailing tabs and spaces
def strip(s, chars=None):
"""strip(s [,chars]) -> string
Return a copy of the string swith leading and trailing
whitespace removed.
If chars is given and not None,remove characters in chars instead.
If chars is unicode, S will beconverted to unicode before stripping.
"""
returns.strip(chars)
#</python>
冒昧的翻译一下: 该方法用来去掉首尾的空格和tab。返回一个去掉空格的S字符串的拷贝。假设參数chars不为None有值,那就去掉在chars中出现的全部字符。假设chars是unicode,S在操作之前先转化为unicode.
以下就上面里子中的sentence1 \2 \3做个说明:
#<python>
str = 'hello world'
print str.strip('heldo').strip()
#</python>
result:wor
运行步骤:
elloworld
lloworld
oworld
oworl
worl
wor
wor
详细代码运行流程:
#<python>
print str.strip('h')
print str.strip('h').strip('e')
print str.strip('h').strip('e').strip('l')
print str.strip('h').strip('e').strip('l').strip('d')
print str.strip('h').strip('e').strip('l').strip('d').strip('o')
print str.strip('h').strip('e').strip('l').strip('d').strip('o').strip('l')
printstr.strip('h').strip('e').strip('l').strip('d').strip('o').strip('l').strip()
#</python>
不知道你是否看懂当中的奥妙,我是在项目经理陕奋勇帮助下,一起才发现这个规律。
如今略微总结一下:
s.strip(chars)使用规则:
首先遍历chars中的首个字符。看看在S中是否处于首尾位置,假设是就去掉。把去掉后的新字符串设置为s,继续循环,从chars中的首个字符開始。假设不在。直接从chars第二个字符開始。一直循环到,s中首尾字符都不在chars中。则循环终止。
关键点:查看chars中字符是否在S中首尾
看完这种方法发现python源代码开发者太牛X了,这么经典算法都想的出。
【饭后糕点】
这种方法主要应用于依照特定规则去除两端的制定字符。假设sentence3就是个非常好的应用。
比如: 截取字符串中两端数字。或者获取特性字符第一次和最后一次出现之间的字符串等等。
Python中strip方法的妙用的更多相关文章
- python中strip()方法学习笔记
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 当使用strip('xxx'),只要字符串头尾有"xxx"中的一个,就会去掉,而不是符合字符串''x ...
- 【Python】PYTHON中STRIP()方法学习笔记
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 当使用strip('xxx'),只要字符串头尾有"xxx"中的一个,就会去掉,而不是符合字符串''x ...
- python中strip,lstrip,rstrip简介
一.起因 今天在做角色控制中,有一个地方用到rstrip,判断用户请求的url是否与数据库对应可用权限中url相符. if request.path == x.url or request.path. ...
- Python中sorted()方法
Python中sorted()方法的用法 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象.i ...
- Python中__init__方法介绍
本文介绍Python中__init__方法的意义. __init__方法在类的一个对象被建立时,马上运行.这个方法可以用来对你的对象做一些你希望的 初始化 .注意,这个名称的开始和结尾 ...
- Python中sorted()方法的用法
Python中sorted()方法的用法 2012-12-24 22:01:14| 分类: Python |字号 订阅 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对i ...
- python中strip函数的用法
python中往往使用剥除函数strip()来对用户的输入进行清理.strip函数的最一般形式为: str.strip('序列') 其中,序列是一段字符串,该函数表示从头或者从尾部开始进行扫描,如果扫 ...
- python中list方法总结
stu=[s1,s2,s3,s4,s5] #list列表/数组 列表的下标/索引是从0开始的: 定义一个列表:XX=[,,,,,] 定义一个空列表:XX=[] or XX=list() #增加一个元素 ...
- python中Strip()函数的用法
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. str.strip([chars]) ...
随机推荐
- BZOJ 4500: 矩阵
4500: 矩阵 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 326 Solved: 182[Submit][Status][Discuss] De ...
- java网络编程学习笔记(四):线程池的实现
package QQ; import java.util.LinkedList; /** * Created by hu on 2015/11/9. */ public class ThreadPoo ...
- [论文]Clustering-Based Ensembles as an Alternative to Stacking
Clustering-Based Ensembles as an Alternative to Stacking 作者:Anna Jurek, Yaxin Bi, Shengli Wu, and Ch ...
- eclipse去除所有调试断点 (转)
今天调试的时候发现之前加了太多断点,想去除所有断点,才想起来一直都没有使用过这个功能,放狗搜了一下,很快找到,记录一下. 方法一: 在工作界面,点window菜单栏,选中Preperences,在Ge ...
- linux 常用命令: runuser
rpm: coreutils-8.4-9.el6.x86_64 runuser --help 用法:runuser [选项]... [-] [用户 [参数]... ] Change the effec ...
- hdu 4970 树状数组 “改段求段”
题意:塔防.给1--n,给出m个塔,每个塔有攻击力,给出k个怪兽的位子和血量,问有几只可以到达n点. 今天刚刚复习了树状数组,就碰到这个题,区间更新.区间求和类型.第三类树状数组可以斩. 注意一下大数 ...
- 洛谷——P1617 爱与愁的一千个伤心的理由
P1617 爱与愁的一千个伤心的理由 题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第二章. 经历了心痛后,爱与愁大神不行了. 题目描述 ...
- Topcoder SRM 664 DIV 1
BearPlays 快速幂 题意: 给你两个数A,B,有种操作是将大的数减去小的数,并将小的数乘以2.反复k次,问你最后的小的数回是多少. 题解: 由于整个过程$A+B$的值是不会改变的.现在令$S= ...
- luogu P2423 双塔
题目描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“911”事件,Mr. F决定自己用水晶来搭建一座双塔.Mr. F有N块水晶,每块 ...
- Mac OS X上使用Wireshark抓包
Wireshark针对UNIX Like系统的GUI发行版界面采用的是X Window(1987年更改X版本到X11).Mac OS X在Mountain Lion之后放弃X11,取而代之的是开源的X ...