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]) ...
随机推荐
- EOJ Monthly 2018.2
A. 坑爹的售票机 题意 用\(1,5,10,25,50,100\)的纸币买\(n\)张单价为\(p\)的船票,且一次性最多买\(k\)张,求钱数恰好时最少需要多少张纸币. Hard: \(n,k,p ...
- tkinter Scale滑块
鼠标拖动和绑定鼠标滚轮移动: import threading from tkinter import * root = Tk() v = StringVar() s1 = Scale(root,fr ...
- hdu 4519(数学题)
郑厂长系列故事——体检 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total S ...
- AC日记——[Sdoi2016]数字配对 bzoj 4514
4514 思路: 很受伤现在,,测了那么多次不过的原因就是因为INF不够大: 解法有两种: 解法1: 把n个点按照质因数个数为奇或偶分为两个点集(很容易就可以想到): 然后,按照题目连边跑最大费用流: ...
- AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
[模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...
- Educational Codeforces Round 35 A. Nearest Minimums【预处理】
[题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...
- luogu P3147 [USACO16OPEN]262144
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- 某考试 T1 monopoly
可以很容易的发现,如果选了最高的房子,那么就不能再选了:否则在左边选一坨合法的,在右边选一坨合法的,拼起来还是合法的. 所以我们可以处理出,每个数的控制区间[L,R] (保证这个区间是其他数都小于它的 ...
- Centos7下实现多虚拟机互信
假设CentOS 7三台虚拟机A(192.168.111.10).B(192.168.111.11).C(192.168.111.12),需要保证三台虚拟机之间网络的连通性. 操作步骤: 一.在A机上 ...
- spring-cloud - 服务之间的通信
上文中已经讲述了基本环境搭建,本文基于上文环境https://www.cnblogs.com/xxpandong/p/10485172.html. spring-cloud中微服务之间通信主要有俩种形 ...