千家信息网

字符串类型内建方法归纳总结

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,版本:python3.6在查看bulitins的内建函数时,对字符串的内建方法进行了分类总结。类别方法描述示例查找string.find(sub, start=None, end=None)在指定的范
千家信息网最后更新 2025年02月03日字符串类型内建方法归纳总结

版本:python3.6

在查看bulitins的内建函数时,对字符串的内建方法进行了分类总结。

类别方法描述示例
查找string.find(sub, start=None, end=None)

在指定的范围内查找子串,

找到则返回最小的index;

未找到则返回-1

mystr = 'hello world 'mystr.find('w') #返回6mystr.find('x') #返回-1
string.rfind(sub, start=None, end=None)

在指定的范围内查找子串,

找到则返回最大的index;

未找到则返回-1

mystr = 'hello world 'mystr.find('l') #返回2mystr.rfind('l') #返回9

string.count(sub, start=None, end=None
)

在指定的范围内查找子串,

找到则返回找的子串个数

未找到则返回0

mystr = 'hello world 'mystr.count('w') #返回3mystr.count('x') #返回0
string.index(sub, start=None, end=None)

在指定的范围内查找子串,

找到则返回最小的index;

未找到则返回ValueError的错误

mystr = 'hello world 'mystr.index('w') #返回6mystr.index('x') #返回ValueError:substring not found
string.rindex(sub, start=None, end=None)

在指定的范围内查找子串,

找到则返回最大的index;

未找到则返回ValueError的错误

mystr = 'hello world 'mystr.index('l')  #返回2mystr.rindex('l')  #返回9
填充、拼接字符string.center(width,fillchar)返回长度为width的字符串,原字符串居中,可指定填充的字符,填充的字符串均分在左右两侧
mystr = 'hello world 'mystr.center(18,'\'')  #返回'''hello world '''
string.join(sep)以字符串为分隔符,将sep中所有的元素合并为一个新的字符串
mystr = ' hello world 'mystr.join('and')#返回a hello world n hello world d
string.zfill(width)返回长度为width的字符串,原字符串右对齐,左侧填充0
mystr = 'hello world 'mystr.zfill(17)  #返回00000hello world

string.ljust(width,fillchar)

返回长度为width的字符串,原字符串左对齐,右侧填充指定的字符
mystr = 'hello world 'mystr.ljust(18,'\'') #返回hello world ''''''
string.rjust(width,fillchar)返回长度为width的字符串,原字符串右对齐,左侧填充指定的字符
mystr = 'hello world 'mystr.rjust(18,'\'') #返回''''''hello world
转换字母大小写string.capitalize()将首字母转换为大写
mystr = 'hello world 'mystr.capitalize() #返回Hello world
string.upper()将字符串中的所有字母转换为大写
mystr = 'hello world 'mystr.upper() #返回HELLO WORLD
string.lower()将字符串中的所有字母转换为小写
mystr = 'Hello World 'mystr.lower() #返回hello world
string.swapcase()翻转字符串中的大小写
mystr = 'Hello World 'mystr.swapcase() #返回hELLO wORLD
string.title()将所有单词的首字母转换为大写
mystr = 'hello world, it\'s fantastic'mystr.title()#返回 Hello World, It'S Fantastic
删除字符串的空格string.strip()删除字符串左右两侧的空格
mystr = '  hello world  'mystr.strip()#返回hello world
string.lstrip()删除字符串左侧的空格
mystr = '  hello world  'mystr.lstrip()#返回hello world
string.rstrip()删除字符串右侧的空格
mystr = '  hello world  'mystr.rstrip()#返回  hello world
拆分字符串string.partition(sep)以指定的字符,将字符串分为为head,sep,tail 3部分;如果没有匹配的字符,返回字符串和2个空串
mystr = ' hello world 'mystr.partition('w')  #返回(' hello ', 'w', 'orld ')mystr.partition(';')  #返回(' hello world ', '', '')
string.rpartition(sep)以指定的字符,将字符串分为为head,sep,tail 3部分;如果没有匹配的字符,返回2个空串和字符串
mystr = ' hello world 'mystr.rpartition('w')  #返回(' hello ', 'w', 'orld ')mystr.rpartition(';')  #返回('', '',' hello world ')
string.split(sep=None, maxsplit=-1)
以sep为分隔符,切片字符串
mystr = ' hello world 'mystr.split(sep='l',maxsplit=2)#返回[' he', '', 'o world ']
string.rsplit(sep=None, maxsplit=-1
)
以sep为分隔符,切片字符串
mystr = ' hello world 'mystr.rsplit(sep='l',maxsplit=2)#返回[' hel', 'o wor', 'd ']
string.splitlines(keepends=None
)
按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符
mystr = ' hello world ,\n it \'s fantastic'mystr.splitlines()#返回[' hello world ,', " it 's fantastic"]
替换string.replace(old, new, count=None
)
字符串替换
mystr = ' hello world 'mystr.replace('world','Beijing')#返回 hello Beijing
is系列string.isalpha()str中至少有一个字符串且所有字符都是字母,则返回True,否则返回Flase
mystr1 = ' hello, world 'mystr2 = 'hello'mystr1.isalpha()  #返回Falsemystr2.isalpha()  #返回True
string.isalnum()str中至少有一个字符串且所有字符都是字母或数字,则返回True,否则返回Flase
mystr1 = ' hello, world ! 'mystr2 = 'hello123'mystr1.isalnum()  #返回Falsemystr2.isalnum()  #返回True
string.isdemical()str中只包含十进制数字则返回True,否则返回False
mystr1 = '1091A 'mystr2 = '1091'mystr1.isdecimal()  #返回Falsemystr2.isdecimal()  #返回True
string.isdigit()str中只包含数字则返回True,否则返回False
mystr1 = '1091A 'mystr2 = '1091'mystr1.isdigit()  #返回Falsemystr2.isdigit()  #返回True
string.isnumeric()str中只包含数字字符则返回True,否则返回False
mystr1 = '1091A 'mystr2 = '1091'mystr1.isnumeric()  #返回Falsemystr2.isnumeric()  #返回True
string.istitle()str是标题化的则返回True,否则返回False
mystr1 = 'hello world 'mystr2 = 'Hello World'mystr1.istitle()   #返回Falsemystr2.istitle()    #返回True
string.islower
()
所有字符都是小写则返回True,否则返回False
mystr1 = 'Hello world 'mystr2 = 'hello world'mystr1.islower()   #返回Falsemystr2.islower()    #返回True
string.isupper()
所有字符都是大写则返回True,否则返回False
mystr1 = 'Hello world 'mystr2 = 'hello world'mystr1.isupper()   #返回False(mystr2.swapcase()).isupper()    #返回True


0