python find() rfind() index() rindex() count() replace()...字符串的各种使用

来源:互联网 发布:监控无网络视频怎么办 编辑:程序博客网 时间:2024/06/06 01:11

苹果电脑快捷键:control+空格:中文;Ctrl为command

内存介于CPU和硬盘之间,CPU运行快,容量小;硬盘容量大,运行速度慢。

一个字符在内存中占有一个字节
str1="hello"
str2="world"

str3=str1+str2#

#hello world 格式化符,这是两种组成字符串的处理方式

#字符串的切片

print('str3')

切片 find() rfind() index() rindex() count() replace()的使用

#encoding=utf-8str1="hello"str2="world"str3=str1+str2#helloworldstrf= "hello %s"%(str2) name="abcedefefageg" #切片的使用name13=name[1:4]#bcestartpos=2endpos=7namese=name[startpos:endpos]# cedef从开始的位置startpos取,到endpos,但是不包括endposfullname=name[:] #复制minuname=name[ :-1]#取到倒数第一个g,abcedefefagegoddname=name[::2]#acdffgg 取字符的间隔为2 [起始位置:结束位置:步长]revername=name[-1:0:-1]#第一个没有取到'rgegafefedecb'revename=name[-1::-1]#逆序:rgegafefedecbarename=name[::-1]#步长为负,从右到左;步长为正,从左向右rgegafefedecba#find 检测字符串name1="learn pyton is a happy thing"pos=name1.find('n') #返回字符串第一次开始出现的位置 4 下标;不存在返回-1rpos=name1.rfind('n')#从字符串右边开始查询 ,第一次出现的下标 26posIndex=name1.index('is')#索引12#find index的相同点:都是查找字符串,返回字符串的下标;区别:find找不到返回-1,index找不到error,出现异常rposIndex=name1.index('is')#12 逆序查找#count count=name1.count('a',0,5)#在1-5之间出现了多少次a(有范围)  1,若无,返回0countFull=name1.count('y') #2  所有出现的次数replacename="I like to learn python"replacepython=replacename.replace('python','C#')#I like to learn C#原字符串没有发生改变,字符串的不变性,返回的字符串发生了改变replaceL=replacename.replace('l','L')#所有的字符都会发生替换 I Like to Learn pythonreplaceCount=replacename.replace('l','L',1)#后面是会替换字符的个数I Like to learn python


split() capitalize() title() startswith() endswith() lower() upper() ljust() rjust() center() lstrip() rstrip() parition() rpartition()的使用

#split 分割,成了列表splitName=name1.split(' ')#按照后面的空格进行切割'learn', 'pyton', 'is', 'a', 'happy', 'thing'splitsName=name1.split()#括号中没有参数时,以出现的除字符以为的多个字符为分隔,如‘ ’ ‘\n’#capitalize 将字符串的第一个字符大写capitalizeName=name1.capitalize()#Learn pyton is a happy thing#title 将字符串的每个单词首字母大写titleName=name1.title()#Learn Pyton Is A Happy Thing'#startswith 检测字符串是否以 后面的字符开头,是返回true,否则返回falsestartWithName=name1.startswith('I')#False#endswith 检测字符串是否以 后面的字符结尾,是返回true,否则返回FALSEendsWithName=name1.endswith('thing')#Truefilename='****.txt'fileTxt=filename.endswith('.txt')#True ,检查后缀#lower将所有的大写字母转为小写字母#upper将所有的小写字母转为大写字母lowerName=name1.lower()#'learn pyton is a happy thing'upperName=name1.upper()#'LEARN PYTON IS A HAPPY THING'end="Yes"if(end.upper()=='YES'):print('OK')#rjust ,返回一个字符串右对齐,并使用空格填充至长度width的新字符串#center 居中显示lJustName=name1.ljust(50)#靠左显示rJustName=name1.rjust(50) #这里开始空格  learn pyton is a happy thingcenterName=name1.center(50) #前后都有空格#去掉空格string='          python is a good language            'lStripString=string.lstrip()#去掉左边的空格rStripString=string.rstrip()#去掉右边的空格stripString=string.strip()#去掉左右两边的空格#partition  将字符以后面的字符为界限,分成三部分,partitionString=string.partition('o')#(('          pyth', 'o', 'n is a good language            ')rPartitonString=string.rpartition('o')#('          python is a ', 'good', ' language            ')

splitlines() isalpha()  isdigit()  isalnum()  isspace()  join()的使用

#splitlines 按照行分隔,返回一个包含各行作为元素的列表stringlines="Python is a good tool \n C# is also a good tool \n the same as java"splitLineString=stringlines.splitlines()#['Python is a good tool ', ' C# is also a good tool ', ' the same as java']#isalpha 判断所有字符都是字母,则返回True,否则返回FalseisalphaString=stringlines.isalpha() #False \n#isdigit 判断是否只包含数字,是返回True,否则返回False#isalnum 判断所有字符是否都是字母或数字,是返回true,否则FalseisDigitString=stringlines.isdigit()#FalseisAlNumString=stringlines.isalnum()#False#isspace() 字符串中只包含空格,返回TruespaceString="    "isSpaceString=spaceString.isspace()#True#join 字符串中每个字符后面插入给定的字符,构造出一个新的字符串#list 和 tuple has no attrbute 'join'List=['a','v','w']joinString=('*').join(List)#'a*v*w'  注意join的用法print(isSpaceString,joinString)
#这里处理一个问题的小技巧#将List的\n ' '为分隔符,再将他们连接成字符#分析:前面学的split()前面的分隔符只指定了一种,这里出现的两种;将分隔后生成的list连接起来,用空格#连接的话,中间会产生空格,所有默认连接sjString="  Today is Monday ,\nand I will go to the park \n"sString=sjString.split()#同时实现了以' '和\n分隔jString=('').join(sString)print(sjString)print(sString)print(js)