python字符串2
来源:互联网 发布:工程设计优化方案 编辑:程序博客网 时间:2024/05/30 04:12
继续…
【2】find方法(rfind倒着来):在一个字符串中查找它的子串
返回第一次出现该子串的索引
语法:
str.find(str, beg=0, end=len(string))
eg:
s = 'hello world'print s.find('l')#输出结果:2
【3】split方法:将字符串切分成若干子串,若无参数,则默认以空格切分
eg:
s = 'hello world'print s.split()#输出结果为:['hello', 'world']
【5】t.join(s)方法:将字符串s用t分割开来,并生成新的字符串
eg:
s = 'hello world'str1 = ','.join(s)print str1#输出结果为:h,e,l,l,o, ,w,o,r,l,d#用','将‘hello world’分割开
【6】s.count()方法:返回一个值,这个值是 子串 str 在字符串s中出现的次数(如果指定开始和结束的索引,则在指定范围内统计)
语法:s.count(str) 或者s.count(str, start , end)
eg:
s = 'haohaohao shu jun shu'count = s.count('shu',0,len(s))print count#输出结果为:2
【7】endswith()方法:返回Ture或False,判断字符串s是否以指定后缀str结尾
语法:s.endswith(str)
eg:
s = 'haohaohao shu jun shu'result1 = s.endswith('jun')result2 = s.endswith('shu')print result1,result2#输出结果为:False True
startswith():返回Ture或False,判断字符串s是否以指定前缀str开始
s = 'haohaohao shu jun shu'result1 = s.startswith('shu')result2 = s.startswith('hao')print result1,result2#输出结果为:False True
【8】rstrip()方法:删除字符串末尾的指定字符
语法:rstrip(char) ——若无参数,默认删除空格
eg:
s = 'shu's = s.rstrip('u')print s#输出结果为:sh
lstrip()方法:截去字符串开始的指定字符(与rstrip同理)
strip()方法:删去字符串头尾的指定字符,若没有指定参数,则默认删除空格
eg:
s = 'ushu's = s.strip('u')print s#输出结果为:sh
###*******************************************
小技巧吧:索引:由于python中(a,b)代表大于等于a,小于b,所以要遍历索引,使用(0,len(s))或者[0:len(s)],具体形式视情况而定;
更一般的是,在选取索引范围时,第一个参数是开始字符的索引值,第二个参数是结束字符的索引值加一
******************************************###
明天打算看看编码问题,先看编码的基础知识吧。
0 0
- Python字符串2
- python字符串2
- python学习--2字符串
- python-2 字符串
- python(2) ---字符串函数
- python coobook 2 ---字符串对齐
- python内存管理2-----字符串
- 2-Python字符串和列表
- python处理字符串(2)
- Python字符串
- Python 字符串
- python字符串
- python 字符串
- 【python】-字符串
- python 字符串
- python字符串
- python 字符串
- python,字符串
- DevC++5.11 调试方法
- Ubuntu下LAMP环境搭建 Apache、MySQL、PHP
- hdu 5929 Basic Data Structure
- yaml-cpp的安装
- 四路红外循迹
- python字符串2
- 稳定排序和不稳定排序
- 自我修行经典书籍
- 穿秋裤啦
- hdu5925 Coconuts
- KMP模式匹配算法
- ssh秘钥配置
- Android Studio查看Gradle版本
- P3143 [USACO16OPEN]钻石收藏家Diamond Collector