Python中关于strip使用方法的小妙招

零到壹度
发布: 2018-04-09 15:25:23
原创
2534人浏览过

本篇文章给大家分享的内容是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>
登录后复制

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

立即学习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>printhelp(__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>
登录后复制

结果见下页:

运行结果:

妙笔工坊
妙笔工坊

妙笔工坊是一个集短剧解说,AI视频生成,口播数字人,小说推文生成的ai智能工具

妙笔工坊 138
查看详情 妙笔工坊
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使用方法的小妙招的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号