• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python中你不知道的strip()函数的妙用

python 搞代码 4年前 (2022-01-09) 22次浏览 已收录 0个评论

【开胃小菜】

当提到python中strip方法,想必凡接触过python的同行都知道它主要用来切除空格。有以下两种方法来实现。

方法一:用内置函数

if name == 'main' :    str = ' Hello world '    print '[%s]' %str.strip()

方法二:调用string模块中方法

import stringif name == 'main' :    str = ' Hello world '    print '[%s]' %string.strip(str)

不知道大家是否知道这两种调用有什么区别?以下是个人一些看法

Ø str.strip()是调用python的内置函数,string.strip(str)是调用string模块中的方法

Ø string.strip(str)是在string模块定义的。而str.strip()是在builtins模块中定义的

问题一: 如何查看一个模块中方法是否在内置模块有定义?

用dir(模块名)看是否有'builtins'属性。

例如:查看string模块

print dir(string)

问题二、 如何查看python中所有的内置函数

  print dir(sys.modules[ 'builtin' ])

问题三 、如何查看内置模块中内置函数定义

 print help(builtins)

以上一些都是大家平时都知道的,接下来就进入本文的主题:

【饭中硬菜】

首先请大家看一下下列程序的运行结果:

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

结果见下页:

运行结果:

worldworldwor2h3h4aaaadgat134f8sfewewrf

你答对了吗?O(∩_∩)O~

如果你都答对了,在此处我奉上32个赞 …

结果分析:

首先我们查看一下string模块中的strip源码:

# Strip leading and trailing tabs and spacesdef strip (s, chars= None ):    """strip(s [,chars]) -> string    Return a copy of the string swith leading and trailing    whitesp<strong style="color:transparent">来源gaodai#ma#com搞@代~码$网</strong>ace removed.    If chars is given and not None,remove characters in chars instead.    If chars is unicode, S will beconverted to unicode before stripping.    """return s.strip(chars)

冒昧的翻译一下: 该方法用来去掉首尾的空格和tab。返回一个去掉空格的S字符串的拷贝。如果参数chars不为None有值,那就去掉在chars中出现的所有字符。如果chars是unicode,S在操作之前先转化为unicode.

下面就上面里子中的sentence1 \2 \3做个说明:

str = 'hello world'print str.strip( ' heldo ' ).strip()
result:wor执行步骤:elloworldlloworldoworldoworl worl worwor

具体代码执行流程:

  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' )    print str.strip( 'h' ).strip( 'e' ).strip( 'l' ).strip( 'd' ).strip( 'o' ).strip( 'l' ).strip()

不知道你是否看懂其中的奥妙,我是在项目经理陕奋勇帮助下,一起才发现这个规律。

现在稍微总结一下:

s.strip(chars)使用规则:

首先遍历chars中的首个字符,看看在S中是否处于首尾位置,如果是就去掉。把去掉后的新字符串设置为s,继续循环,从chars中的首个字符开始。如果不在,直接从chars第二个字符开始。一直循环到,s中首尾字符都不在chars中,则循环终止。

关键点:查看 chars 中字符是否在 S 中首尾

看完这个方法发现python源码开发人员太牛X了,这么经典算法都想的出。

【饭后糕点】

这个方法主要应用于按照特定规则去除两端的制定字符。如果sentence3就是个很好的应用。

例如: 截取字符串中两端数字,或者获取特性字符第一次和最后一次出现之间的字符串等等。

【相关推荐】

1. Python免费视频教程

2. python中strip()鲜为人知的陷阱

3. python基础入门之教你如何用strip()函数 去空格\n\r\t

4. 详解python中strip()和split()的使用方法

5. 详解python中strip函数的使用场景

以上就是Python中你不知道的strip()函数的妙用的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python中你不知道的strip()函数的妙用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址