字符串类型内建方法归纳总结
发表于: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 |
字符
字符串
字母
范围
大写
数字
空格
长度
分隔符
方法
最大
最小
元素
右侧
大小
小写
少有
换行符
空串
部分
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
企业网络安全如何监控
工信部通信行业网络安全
观看网络安全警示教育心得体会
数据库错误代码2053
游爱网络技术股份有限公司
上海软件开发排名
极限在线网络技术
离散数学答案软件开发
发电站网络安全证
人机三位一体网络安全
江西信息化软件开发服务参考价格
福州天创网络技术
服务器有必要做主机吗
互联网科技 工作相关
手机软件开发经理招聘
手机怎么连接到服务器
国产串口服务器生产
软件开发学什么技术
农商网络安全知识竞赛
网络安全整改工作情况汇报
浦东新区网络安全硬件设备厂家
微笑数据库损坏卸载
仓库管理系统服务器版
金华软件开发哪家公司好
软件开发商缴税
我的世界服务器大全
手机改网游服务器数据
软件开发专业出来能做什么的
网络安全和信息化中心待遇
汇聚网络技术有限公司