【每天1分钟】PYTHON基础之数据类型-字符串(检查/查找)
2019-07-18 00:00
531 查看
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)
【每天1分钟】PYTHON基础之数据类型-字符串(检查/查找)
- count()
统计字符出现次数
>>> str = 'abbcccdddd' >>> str.count('a') 1 >>> str.count('bb') 1 >>> str.count('c') 3 >>> str.count('ddd') 1 >>> str.count('e') 0 >>>
- find/rfind()
查找字符串
>>> str = 'abcdedcba' >>> str.find('b') 1 >>> str.rfind('b') 7 >>>
- index/rindex()
字符串对应的索引值
>>> str = 'abbcccddddeddddcccbba' >>> str.index('a') 0 >>> str.index('b') 1 >>> str.rindex('a') 20 >>> str.rindex('b') 19 >>> str.index('f') Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> str.index('f') ValueError: substring not found >>> str.rindex('f') Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> str.rindex('f') ValueError: substring not found >>>
- endswith/startswith()
开始于 / 结束于
>>> str = 'abbcccddddeddddcccbba' >>> str.endswith('a') True >>> str.startswith('a') True >>> str.endswith('e') False >>> str.startswith('e') False >>> str.endswith('ba') True >>> str.startswith('ab') True >>>
- isalnum()
检测字符串是否由字母和数字组成
>>> str = 'iamaboy' >>> str.isalnum() # 只有字母和数字,返回True True >>> str = "i am a boy" # 字符串中有空格 >>> str.isalnum() False >>>
- isalpha()
检测字符串是否只由字母组成
>>> str = 'iamaboy' >>> str.isalpha() True >>> str = 'iamaboy2' >>> str.isalpha() #2不是字母 False >>>
- isdigit()
检测字符串是否只由数字组成
>>> str = "123456" >>> str.isdigit() True >>> str = 'iamaboy' >>> str.isdigit() False >>>
- islower()
检测字符串是否由小写字母组成
>>> str = "I am a boy, i am 3 year's old." >>> str.islower() False >>> str = "i am a boy, i am 3 year's old." >>> str.islower() True >>>
- isspace()
检测字符串是否只由空格组成
>>> str = " " >>> str.isspace() True >>> str = " * " >>> str.isspace() False >>>
- istitle()
检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写
>>> str = "This Is String Example...Wow!!!" >>> str.istitle() True >>> str = "This is string example....wow!!!" >>> str.istitle() False >>>
- isupper()
检测字符串中所有的字母是否都为大写
>>> str = "THIS IS STRING EXAMPLE....WOW!!!" >>> str.isupper() True >>> str = "THIS is string example....wow!!!" >>> str.isupper() False >>>
相关文章推荐
- 【每天1分钟】PYTHON基础之数据类型-字符串(修改内容)
- Python基础4:数据类型:数字 字符串 日期
- Python学习笔记(一)--Python基础知识:变量、数据类型、模块与字符串
- Python基础——核心数据类型_序列_字符串
- Python基础学习----数据类型,字符串,内置数据结构
- python基础:基本数据类型:整数,浮点数,字符串
- Python3学习(一)-基础、数据类型、变量、字符串和编码、list&tuple、if、for、while、dict、set、函数与参数
- Python新手学习基础之数据类型——字符串的切片截取
- Python入门(二)——IDE选择PyCharm,输入和输出,基础规范,数据类型和变量,常量,字符串和编码,格式化
- Python【基础:数据类型和变量 字符串和编码 list和tuple 条件判断 循环 dict和set】注意事项(与java,c比较)
- Python基础一_数据类型与字符串编码
- Python基础:数据类型-字符串:string
- python 基础数据类型之字符串02
- Python基础——原生数据类型(字典,列表,元组,字符串)
- python基础数据类型(一):字符串
- python基础一数据类型之字符串
- Python新手学习基础之数据类型——字符串类型
- Python新手学习基础之数据类型——字符串类型
- Python基础:1.数据类型(空、布尔类型、整型、长整型、浮点型、字符串)
- Python新手学习基础之数据类型——字符串类型