Python字符串(string)常用函数 |
您所在的位置:网站首页 › 连接字符串的函数python › Python字符串(string)常用函数 |
Python字符串(string)常用函数
find:检测字符串是否包含指定字符,如果存在则返回开始的索引值,否则返回-1 str1 = 'hello world' print(str1.find('w')) #存在,则返回是该字符串位置 print(str1.find('z')) #不存在,则返回-1 返回结果: 6 -1index:检测字符串是否包含指定字符,若果存在返回开始的索引值,否则提示错误信息 str1 = 'hello world' print(str1.index('d')) #存在,则返回该字符串位置 print(str1.index('z')) #不存在,提示错误信息 返回结果: 10 print(str1.index('z')) #不存在,提示错误信息 ValueError: substring not foundcount:返回字符串指定索引范围内[start,end]出现的次数 str1 = 'hello world' print(str1.count('o')) #统计该字符串出现的次数 print(str1.count('l',2,10)) #指定位置查找,指定第2到第10位字符串之间 返回结果: 2 3replace:替换,将str1替换成str2,如果指定count,则不超过count次 str1 = 'hello world hello python' print(str1.replace('hello','go out')) #将hello替换为go out 返回结果: go out world go out pythonsplit:切割,指定值进行切割 str1 = 'hello world hello python hello china hello hello hello hello' print(str1.split('o')) #指定字符进行切割,默认输出到列表中 print(str1.split('o',3)) #指定字符串进行切割,从指定位置之后的不进行切割 返回结果: ['hell', ' w', 'rld hell', ' pyth', 'n hell', ' china hell', ' hell', ' hell', ' hell', ''] ['hell', ' w', 'rld hell', ' python hello china hello hello hello hello']capitalize:将字符串的首字母大写 str1 = 'hello world hello python' print(str1.capitalize()) 返回结果: Hello world hello pythontitle:将字符串中每个单词的首字母进行大写 str1 = 'hello world' print(str1.title()) #将每个单词的首字母大写 返回结果: Hello Worldstartswith:检查字符串是否是指定字符开头,是则返回True,否则返回False str1 = 'hello world hello china' print(str1.startswith('hello')) #是,则返回True print(str1.startswith('world')) #否,则返回False 返回结果: True Falseendswith:检查字符串是否是指定字符结尾,是则返回True,否则返回False str1 = 'hello world hello china' print(str1.endswith('china')) #是,则返回Ture print(str1.endswith('hello')) #否,则返回False 返回结果: True Falselower:将字符串转换为小写 str1 = 'Hello World HELLO CHINA' print(str1.lower()) #将字符串转换为小写 返回结果: hello world hello chinaupper:将字符串转换为大写 str1 = 'hello world hello china' print(str1.upper()) #将字符串转换为大写 返回结果: HELLO WORLD HELLO CHINAljust:返回一个字符串左对齐,并使用空格填充至长度width的新字符串 str1 = 'hello' print(str1.ljust(10)) #左对齐,字符右侧由空格填充至指定长度 返回结果: hello #选中查看效果rjust:返回一个字符串右对齐,并使用空格填充至长度width的新字符串 str1 = 'hello' print(str1.rjust(10)) #右对齐,字符左侧由空格填充至指定长度 返回结果: hello #选中查看效果center:返回一个源字符串居中,并使用空格填充至长度width的新字符串 str1 = 'hello' print(str1.center(15)) #居中对齐,字符左右两侧由空格填充至指定长度 返回结果: hello #选中查看效果lstrip:去除字符串左边空白字符 str1 = ' hello' print(str1) #默认输出字符串结果左边携带空格字符 print(str1.lstrip()) #去除字符串左边空格后结果 返回结果: hello hellorstrip:去除字符串右边空白字符 str1 = 'hello ' print(str1) #默认输出字符串结果右边携带空格字符 print(str1.rstrip()) #去除字符串右边空格后结果 返回结果: hello #默认情况 hellostrip:去除字符串两遍空白字符 str1 = ' hello ' print(str1) #默认输出字符串结果左右两遍携带空格字符 print(str1.strip()) #去除后字符串左右两遍空格后结果 返回结果: hello #默认情况 hellopartition:可以根据指定将字符串进行分割,成为三个部分 str1 = 'hello world hello china' print(str1.partition('world')) #指定字符串进行分割为三部分 返回结果: ('hello ', 'world', ' hello china')join:在str2中每个两个字符中间面插入str1中内容,构造出一个新的字符串 str1 = '_' str2 = ['hello','world','hello'] #在每两个字符中间插入一个str1中内容,使之组成新的字符 print(str1.join(str2)) 返回结果: hello_world_helloisspace:如果str1中只包含空格,则返回True,否则返回False str1 = ' ' str2 = ' hello' print(str1.isspace()) print(str2.isspace()) 返回结果: True Falseisdigit:如果str1只包含数字则返回True,否则返回False str1 = 'a123' str2 = '123' print(str1.isdigit()) #返回False,包含字母 print(str2.isdigit()) #返回True,只包含数字 返回结果: False Trueisalnum:如果str1所有字符都是字母或数字则返回True,否则返回False str1 = 'a123' print(str1.isalnum()) #返回True,只包含数字 返回结果: Trueisalpha:如果str1所有字符都是都是字母,则返回True,否则返回False str1 = 'abc' print(str1.isalpha()) #返回True,只包含字母 返回结果: True |
今日新闻 |
点击排行 |
|
推荐新闻 |
|
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |