字符串类型内建方法归纳总结
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,版本:python3.6在查看bulitins的内建函数时,对字符串的内建方法进行了分类总结。类别方法描述示例查找string.find(sub, start=None, end=None)在指定的范
千家信息网最后更新 2025年12月02日字符串类型内建方法归纳总结
版本: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 |
字符
字符串
字母
范围
大写
数字
空格
长度
分隔符
方法
最大
最小
元素
右侧
大小
小写
少有
换行符
空串
部分
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
游戏和软件开发
sql数据库数据怎么打开
南宁系统软件开发
pdu服务器电源生产厂家
山东泽付通网络技术
福建狂潮软件开发公司
深圳vr软件开发培训
天津智能医疗系统软件开发
关系数据库导入hive
光峰科技互联网
广西时代网络技术分类创新服务
威纶屏怎么做数据库
股票点买软件开发
石景山区专业性软件开发优势
黑蚁网络技术
软件开发岗笔试题目
主播平台软件开发
黑魂三服务器没了还能联机吗
网络安全的程度
新时达服务器故障
芜湖狼道网络技术有限公司
网络安全小知识观后感第197期
阿里云学生服务器释放
浪潮服务器上市公司
网络安全 传媒
中小学生作业数据库
手机软件开发的软件测试
大数据网络技术基础课程
网络安全威胁主要来自那些
深圳人脸软件开发