Python字符串(string)常用函数

find:检测字符串是否包含指定字符,如果存在则返回开始的索引值,否则返回-1

str1 = 'hello world'
print(str1.find('w'))   #存在,则返回是该字符串位置
print(str1.find('z'))   #不存在,则返回-1
返回结果:
6
-1

index:检测字符串是否包含指定字符,若果存在返回开始的索引值,否则提示错误信息

str1 = 'hello world'
print(str1.index('d'))  #存在,则返回该字符串位置
print(str1.index('z'))  #不存在,提示错误信息
返回结果:
10
print(str1.index('z'))  #不存在,提示错误信息
ValueError: substring not found

count:返回字符串指定索引范围内[start,end]出现的次数

str1 = 'hello world'
print(str1.count('o'))  #统计该字符串出现的次数
print(str1.count('l',2,10)) #指定位置查找,指定第2到第10位字符串之间
返回结果:
2
3

replace:替换,将str1替换成str2,如果指定count,则不超过count次

str1 = 'hello world hello python'
print(str1.replace('hello','go out'))   #将hello替换为go out
返回结果:
go out world go out python

split:切割,指定值进行切割

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 python

title:将字符串中每个单词的首字母进行大写

str1 = 'hello world'
print(str1.title()) #将每个单词的首字母大写
返回结果:
Hello World

startswith:检查字符串是否是指定字符开头,是则返回True,否则返回False

str1 = 'hello world hello china'
print(str1.startswith('hello')) #是,则返回True
print(str1.startswith('world')) #否,则返回False
返回结果:
True
False

endswith:检查字符串是否是指定字符结尾,是则返回True,否则返回False

str1 = 'hello world hello china'
print(str1.endswith('china'))   #是,则返回Ture
print(str1.endswith('hello'))   #否,则返回False
返回结果:
True
False

lower:将字符串转换为小写

str1 = 'Hello World HELLO CHINA'
print(str1.lower())  #将字符串转换为小写
返回结果:
hello world hello china

upper:将字符串转换为大写

str1 = 'hello world hello china'
print(str1.upper())    #将字符串转换为大写
返回结果:
HELLO WORLD HELLO CHINA

ljust:返回一个字符串左对齐,并使用空格填充至长度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
hello

rstrip:去除字符串右边空白字符

str1 = 'hello      '
print(str1)     #默认输出字符串结果右边携带空格字符
print(str1.rstrip())    #去除字符串右边空格后结果
返回结果:
hello    #默认情况  
hello

strip:去除字符串两遍空白字符

str1 = '    hello    '
print(str1)     #默认输出字符串结果左右两遍携带空格字符
print(str1.strip())     #去除后字符串左右两遍空格后结果
返回结果:
    hello    #默认情况
hello

partition:可以根据指定将字符串进行分割,成为三个部分

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_hello

isspace:如果str1中只包含空格,则返回True,否则返回False

str1 = ' '
str2 = ' hello'
print(str1.isspace())
print(str2.isspace())
返回结果:
True
False

isdigit:如果str1只包含数字则返回True,否则返回False

str1 = 'a123'
str2 = '123'
print(str1.isdigit())   #返回False,包含字母
print(str2.isdigit())   #返回True,只包含数字
返回结果:
False
True

isalnum:如果str1所有字符都是字母或数字则返回True,否则返回False

str1 = 'a123'
print(str1.isalnum())   #返回True,只包含数字
返回结果:
True

isalpha:如果str1所有字符都是都是字母,则返回True,否则返回False

str1 = 'abc'
print(str1.isalpha())   #返回True,只包含字母
返回结果:
True
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐