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]) ...
随机推荐
- jsp导出table数据excel表
<html> <head> <meta http-equiv="content-Type" content="text/html;chars ...
- 气象城市ID列表
气象城市ID列表 数据来源: http://cj.weather.com.cn/support/Detail.aspx?id=51837fba1b35fe0f8411b6df 记录了2574个地区,2 ...
- 汉化CodeBlock
codeblock最新版本发布了,但是对一些看不惯英文的来说,还是中文好点. 一.准备工作,先下载codeblock最新版,可以从官方下载,也可以从http://www.uzzf.com/soft/1 ...
- C#图解教程学习笔记——事件
一.事件的定义事件:当一个特定的程序事件发生时,程序的其他部分可以得到该事件已经发生的通知,同时运行相应处理程序.事件的很多部分都与委托类似.实际上,事件就像专门用于特殊用途的简单委托.事件包含了一个 ...
- HTML5-contentEditable属性规定是否可编辑元素的内容
contentEditable='true' :可以编辑 contentEditable='false' :不可以编辑
- 10.1综合强化刷题 Day4
财富(treasure) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有n个小伙伴.每个小伙伴有一个身高hi. 这个游戏是这样的,LYK生活的环境是以 ...
- 2017省选集训测试赛(二十五)Problem B recollection
@(XSY)[后缀数组, 启发式合并, ST表] Description Solution 后缀数组 + 启发式合并 + Sparse Table. 这是第一次写树上后缀数组. 对以每个点为根的子树统 ...
- 原生js获取元素的样式信息
工作中经常会需要获取DOM元素的样式,之前都是通过jquery的css()方法,现在总结一下通过原生js获取元素样式的方法. obj.style js var _width = obj.style.w ...
- Caught exception while loading file struts-default.xml 的错误
转自刘长炯的博客:http://www.blogjava.net/beansoft/archive/2008/10/13/233962.html MyEclipse 6开发JDK6和Struts 2冲 ...
- 在html里网页中嵌入优酷的视频
<html> <embed src="http://player.youku.com/player.php/sid/XMjAzOTk4NjI4/v.swf" qu ...