网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

版本:python3.6

创新互联建站是一家专注于成都网站建设、做网站与策划设计,富源网站建设哪家好?创新互联建站做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:富源等地区。富源做网站价格咨询:18982081108

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

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

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

找到则返回最小的index;

未找到则返回-1

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

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

找到则返回最大的index;

未找到则返回-1

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

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

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

找到则返回找的子串个数

未找到则返回0

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

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

找到则返回最小的index;

未找到则返回ValueError的错误

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

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

找到则返回最大的index;

未找到则返回ValueError的错误

mystr = 'hello world '
mystr.index('l')  #返回2
mystr.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()  #返回False
mystr2.isalpha()  #返回True
string.isalnum()str中至少有一个字符串且所有字符都是字母或数字,则返回True,否则返回Flase
mystr1 = ' hello, world ! '
mystr2 = 'hello123'
mystr1.isalnum()  #返回False
mystr2.isalnum()  #返回True
string.isdemical()str中只包含十进制数字则返回True,否则返回False
mystr1 = '1091A '
mystr2 = '1091'
mystr1.isdecimal()  #返回False
mystr2.isdecimal()  #返回True
string.isdigit()str中只包含数字则返回True,否则返回False
mystr1 = '1091A '
mystr2 = '1091'
mystr1.isdigit()  #返回False
mystr2.isdigit()  #返回True
string.isnumeric()str中只包含数字字符则返回True,否则返回False
mystr1 = '1091A '
mystr2 = '1091'
mystr1.isnumeric()  #返回False
mystr2.isnumeric()  #返回True
string.istitle()str是标题化的则返回True,否则返回False
mystr1 = 'hello world '
mystr2 = 'Hello World'
mystr1.istitle()   #返回False
mystr2.istitle()    #返回True
string.islower
()
所有字符都是小写则返回True,否则返回False
mystr1 = 'Hello world '
mystr2 = 'hello world'
mystr1.islower()   #返回False
mystr2.islower()    #返回True
string.isupper()
所有字符都是大写则返回True,否则返回False
mystr1 = 'Hello world '
mystr2 = 'hello world'
mystr1.isupper()   #返回False
(mystr2.swapcase()).isupper()    #返回True

文章名称:字符串类型内建方法归纳总结
当前URL:http://bjjierui.cn/article/jppggd.html

其他资讯