【开胃小菜】

当提到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方法的妙用的更多相关文章

  1. python中strip()方法学习笔记

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 当使用strip('xxx'),只要字符串头尾有"xxx"中的一个,就会去掉,而不是符合字符串''x ...

  2. 【Python】PYTHON中STRIP()方法学习笔记

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 当使用strip('xxx'),只要字符串头尾有"xxx"中的一个,就会去掉,而不是符合字符串''x ...

  3. python中strip,lstrip,rstrip简介

    一.起因 今天在做角色控制中,有一个地方用到rstrip,判断用户请求的url是否与数据库对应可用权限中url相符. if request.path == x.url or request.path. ...

  4. Python中sorted()方法

    Python中sorted()方法的用法 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象.i ...

  5. Python中__init__方法介绍

    本文介绍Python中__init__方法的意义.         __init__方法在类的一个对象被建立时,马上运行.这个方法可以用来对你的对象做一些你希望的 初始化 .注意,这个名称的开始和结尾 ...

  6. Python中sorted()方法的用法

    Python中sorted()方法的用法 2012-12-24 22:01:14|  分类: Python |字号 订阅 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对i ...

  7. python中strip函数的用法

    python中往往使用剥除函数strip()来对用户的输入进行清理.strip函数的最一般形式为: str.strip('序列') 其中,序列是一段字符串,该函数表示从头或者从尾部开始进行扫描,如果扫 ...

  8. python中list方法总结

    stu=[s1,s2,s3,s4,s5] #list列表/数组 列表的下标/索引是从0开始的: 定义一个列表:XX=[,,,,,] 定义一个空列表:XX=[] or XX=list() #增加一个元素 ...

  9. python中Strip()函数的用法

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. str.strip([chars]) ...

随机推荐

  1. HDU [P5015] 233 Matrix

    矩阵快速幂 按列递推 #include <iostream> #include <cstdio> #include <cstdlib> #include <a ...

  2. Xor路

    Xor路 (xor.pas/c/cpp)128MB1s 给定一棵有N个点和N-1条边的树,请你求出树中的最长路径,以及总共有多少条最长路径. 这里路径长度是用xor定义的,即若经过的边的权值为a1, ...

  3. Codeforces 920D Tanks

    题目链接 题意 有 \(n\) 个容积无限的水缸,初始时水量为\(a_1,a_2,...,a_n\),有一把容积为\(k\)的勺子,可以从一个水缸中舀水倒入另一个水缸中.问能否给出操作序列,使得最终某 ...

  4. svn没有"对号"等符号

    [问题描述]调整svn建立好了服务端.安装客户端也检出成功了.但是就是没有对号符号. [解决方案]右键菜单,设置,里面有“图标覆盖”这个选项,把你的文件夹加入进去,然后注销windows用户重新登陆

  5. hdu 4970 树状数组 “改段求段”

    题意:塔防.给1--n,给出m个塔,每个塔有攻击力,给出k个怪兽的位子和血量,问有几只可以到达n点. 今天刚刚复习了树状数组,就碰到这个题,区间更新.区间求和类型.第三类树状数组可以斩. 注意一下大数 ...

  6. Node.js应用场景及发展趋势

    node主要应用场景是在大前端,阿里的思路是比较合适的,但是必须要注意,绝对不能让node做太多的业务逻辑,他只适合接受人家生成好的数据,然后或渲染后,或直接发送到客户端.如果让node做复杂的业务逻 ...

  7. 牛客网 Wannafly挑战赛8 B.LBJX的三角形

    B-LBJX的三角形 链接:https://www.nowcoder.com/acm/contest/57/B来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K, ...

  8. Redis 批量删除Redis的key 正则匹配删除

    del 删除单个key方便 要是删除多个就不是很方便了 这时候可以使用xsrsg来批量删除 1.退出redis 2.匹配CCPAI:开头的所有key*删除 redis-cli -a 密码 -h hos ...

  9. 任意选若干个不相邻的数得到的和最大【dp】

    非相邻数最大和 ///*任意选若干个不相邻的数得到的和最大*/ #include<cstdio> #include<cstring> #include<queue> ...

  10. Oracle数据库搭建