Python探索记(10)——字符串(下)
来源:互联网 发布:mfc编程实例 2008 编辑:程序博客网 时间:2024/06/05 17:48
关于字符串的常见操作,请参见如下示例:
# @Time : 2017/7/2 21:26# @Author : 原创作者:谷哥的小弟# @Site : 博客地址:http://blog.csdn.net/lfdfhl# @DESC : String常见操作string='hello,my name is hanmeimei''''find()判断子串是否在String中,若存在则返回子串在String中开始的索引值,否则返回-1rfind()类似于find()函数,不过是从右边开始查找.'''index1=string.find('llo')print('index1=',index1)#利用第二个,第三个参数指定判断的范围index2=string.find('llo',0,10)print('index2=',index2)index3=string.find('llo',7,13)print('index3=',index3)print('= '*15)'''count()统计子串在String中出现的次数'''count1=string.count('m')#利用第二个,第三个参数指定统计的范围count2=string.count('m',0,8)print('count1=',count1)print('count2=',count2)print('= '*15)'''replace()把string中的str1替换成str2,如果count指定,则替换次数不超过count'''replaceResult1=string.replace('mei','XX')print('replaceResult1=',replaceResult1)replaceResult2=string.replace('mei','XX',1)print('replaceResult2=',replaceResult2)print('string=',string)print('= '*15)'''split()以str为分隔符切割string,可用maxsplit指定分割次数,即分隔成 maxsplit + 1 个子字符串'''fragment1=string.split(' ')print('fragment1=',fragment1)fragment2=string.split(' ',2)print('fragment2=',fragment2)print('= '*15)'''startswith()判断string是否以str开头'''result1=string.startswith('hello')result2=string.startswith('ello')print('result1=',result1)print('result2=',result2)print('= '*15)'''endsWith()判断string是否以str结尾'''result3=string.endswith('mei')result4=string.endswith('xxx')print('result3=',result3)print('result4=',result4)print('= '*15)'''upper()将string中字母变成大写'''upperString=string.upper()print('upperString=',upperString)print('= '*15)'''lower()将string中字母变成小写'''lowerString=upperString.lower()print('lowerString=',lowerString)print('= '*15)'''strip()删除字符串两端的空格。类似的方法有lstrip(),rstrip()删除字符串左侧,右侧的空格'''name=' Hello all 'stripResult=name.strip()print('stripResult=',stripResult)print('= '*15)'''partition()把string以str分割成三部分,str前,str和str后rpartition()类似于 partition()函数,不过是从右边开始.'''fragments=string.partition('llo')print('fragments=',fragments)print('= '*15)'''splitlines()按照行分隔即\n,返回一个包含各行作为元素的列表'''lines="good\nmorning\nsir"resultLines=lines.splitlines()print('resultLines=',resultLines)print('= '*15)'''isalpha()判断string中是否全为字母'''isalpha=string.isalpha()print('isalpha=',isalpha)print('= '*15)'''isdigit()判断string中是否全为数字'''digitString='123456789'isdigit=digitString.isdigit()print('isdigit=',isdigit)print('= '*15)'''join()str.join(sequence)将字符串、元组、列表中的元素以指定的字符(str)连接生成一个新的字符串'''str='_'sequence=['Hello','Everyone','Thanks']joinResult=str.join(sequence)print('joinResult=',joinResult)print('= '*15)
测试结果如下:
index1= 2index2= 2index3= -1= = = = = = = = = = = = = = = count1= 4count2= 1= = = = = = = = = = = = = = = replaceResult1= hello,my name is hanXXXXreplaceResult2= hello,my name is hanXXmeistring= hello,my name is hanmeimei= = = = = = = = = = = = = = = fragment1= ['hello,my', 'name', 'is', 'hanmeimei']fragment2= ['hello,my', 'name', 'is hanmeimei']= = = = = = = = = = = = = = = result1= Trueresult2= False= = = = = = = = = = = = = = = result3= Trueresult4= False= = = = = = = = = = = = = = = upperString= HELLO,MY NAME IS HANMEIMEI= = = = = = = = = = = = = = = lowerString= hello,my name is hanmeimei= = = = = = = = = = = = = = = stripResult= Hello all= = = = = = = = = = = = = = = fragments= ('he', 'llo', ',my name is hanmeimei')= = = = = = = = = = = = = = = resultLines= ['good', 'morning', 'sir']= = = = = = = = = = = = = = = isalpha= False= = = = = = = = = = = = = = = isdigit= True= = = = = = = = = = = = = = = joinResult= Hello_Everyone_Thanks= = = = = = = = = = = = = = =
阅读全文
0 0
- Python探索记(10)——字符串(下)
- Python探索记(09)——字符串(上)
- Python探索记(02)——变量
- Python探索记(06)——while
- Python探索记(07)——for
- Python探索记(17)——函数
- Python探索记(14)——字符串、列表、元组、字典与运算符相关的操作
- Python探索记(15)——Python内置函数
- PHP探索之旅—-字符串String
- Python探索记(03)——输入和输出
- Python探索记(04)——运算符
- Python探索记(05)——if和else
- Python探索记(08)——break和continue
- Python探索记(11)——列表List
- Python探索记(12)——元组Tuple
- Python探索记(13)——字典Dictionary
- Python探索记(18)——文件File
- Python探索记(01)——HelloWorld及Python的注释
- angularjs指令的独立作用域和绑定策略
- fork()系统调用的特性
- Ubuntu16 编译Android5.1 lollipop 源码出错 unsupported reloc 43
- ViewPager的使用方法
- ImageView的平铺效果
- Python探索记(10)——字符串(下)
- 2.策略模式
- 如何在office word or ppt 中插入Latex公式
- Android:借用html标签协议,定义自己的协议
- Eclipse修改工作空间--配置必备
- JS-String对象的方法(上)
- hdu_round1-1003 yyf和女朋友玩游戏
- Web前端-HeadFirst笔记-CH10 span div 伪类
- 支持向量机通俗导论(理解SVM的三层境界)