资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

字符串类型内建方法归纳总结-创新互联

版本:python3.6

公司主营业务:成都网站设计、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出青州免费做网站回馈大家。

在查看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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:字符串类型内建方法归纳总结-创新互联
网页链接:http://cdkjz.cn/article/digsdp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220