Python 字符串str类型的方法(内建函数)分类举例列表
2023-09-14 09:01:28 时间
字符串类型str方法的内建函数 分类列表
str | 注释 | 基础用法举例 (s='abcdef') |
__len__ | 串长 len(s) | s.__len__() # 6 len(s) # 6 |
__iter__ | 迭代器 | for i in s.__iter__():print(i) #遍历字符 for i in s:print(i) #同上 |
__getitem__ | 索引串的字符 s[i] | s.__getitem__(-1) # ’f' s.__getitem__(5) # ’f',索引号<len(s) |
__contains__ | 是否包含某字符 | s.__contains__('a') # True s.__contains__('g') # False |
capitalize | 首字母改为大写 | S=s.capitalize() # S='Abcdef' |
lower | 串中字母全改为小写 | >>> 'ABCd'.lower() 'abcd' |
islower | 判断串中字母是否全为小写 | >>> s.islower() #True >>> S.islower() #False |
upper | 串中字母全改为大写 | >>> 'abcD'.upper() 'ABCD' |
isupper | 判断串中字母是否全为大写 | >>> 'ABCD'.isupper() #True >>> 'abcD'.isupper() #False |
title | 标题化字串所有单词首字母大写 | >>>'i love you, 323'.title() 'I Love You, 323' #非字母不受影响 |
istitle | 判断字串是否为标题(仅首字母) | >>> 'I Love You'.istitle() #True >>> 'I Love YOU'.istitle() #False |
swapcase | 串中字母大小写互换 | >>> 'I Love You'.swapcase() 'i lOVE yOU' |
casefold | ||
zfill | 左边填充0到指定长度 | >>> s.zfill(12) '000000abcdef' |
center | 居中填充,默认填充字符为空格 | >>> s.center(12) # ' abcdef ' >>> s.center(12,'*') # '***abcdef***' |
count | 返回子串的个数 | >>> 'abac'.count('a') # 2 >>> 'abac'.count('b') # 1 |
encode | ||
expandtabs | ||
index | 子串索引,不存在会异常退出 | >>> s.index('bc') # 1 >>> s.index('bd') # ValueError 退出不执行后面的语句 |
rindex | 从字串右边开始索引子串 | >>> s.rindex('bc') # 1 子串只有一个返回值与上同 >>> 'abac'.index('a') # 2 |
find | 查找子串,不存在返回-1 | >>> s.find('bc') # 1 >>> s.find('bd') # -1 |
rfind | 从字串右边开始查找子串 | >>> 'abac'.rfind('a') # 2 >>> 'abac'.rfind('d') # -1 |
format | ||
format_map | ||
replace | ||
isalnum | ||
isalpha | ||
isascii | ||
isdecimal | ||
isdigit | ||
isidentifier | ||
isnumeric | 判断是否为数字 | >>> t='0123456789a' >>> [_.isnumeric() for _ in t] # == [True]*10 + [False] |
isprintable | 判断是否为可打印字符 | |
isspace | 判断是否为空格符 | >>> t=' \t\n\r\v\f' >>> [_.isspace() for _ in t] # == [True]*6 >>> [_.isprintable() for _ in t] # == [True] + [False]*5 |
join | 用分隔串连接序列成字符串 | >>> '>'.join(['a','b','c']) 'a>b>c' |
ljust | 左对齐 | |
rjust | 右对齐 | |
split | 分拆字符串成序列 | |
rsplit | 分拆字符串成序列 | |
splitlines | ||
partition | ||
rpartition | ||
maketrans | ||
translate | ||
startswith | 判断是否存在前缀子串 | >>> 'www.csdn.net'.startswith('www') #True >>> 'www.csdn.net'.startswith('http') #False |
endswith | 判断是否存在后缀子串 | >>> 'www.csdn.net'.endswith('com') #False >>> 'www.csdn.net'.endswith('net') #True |
strip | 消除字串左右两边的空格 | >>> ' a b c '.strip() 'a b c' |
lstrip | 只消除字串左边的空格 | >>> ' a b c '.lstrip() 'a b c ' |
rstrip | 只消除字串右边的空格 | >>> ' a b c '.rstrip() ' a b c' |
相关文章
- python re.compile() 详解——Python正则表达式「建议收藏」
- 如何在pycharm中安装pygame_python安装pycharm的方法
- python字符串转化列表_Python列表到字符串的转换[通俗易懂]
- 情人节来了,教你个用 Python 表白的技巧「建议收藏」
- Python列表(list)及其常用方法
- python-列表(list)使用方法
- python开发中重写父类的方法有哪些?
- 数据归一化及三种方法(python)
- Python编程 列表的常用方法
- 【说站】python字符串中有哪些方法
- 【说站】Python threading模块的常用方法
- 【说站】python绘制散点图的两种方法
- Python保存json_python保存json文件
- python列表添加元素的方法_列表添加元素的三种方法
- python win32api sendmessage_Python win32api.SendMessage方法代码示例[通俗易懂]
- 【7】python_matplotlib 输出(保存)矢量图方法;画图时图例说明(legend)放到图像外侧;Python_matplotlib图例放在外侧保存时显示不完整问题解决
- Python列表常用的函数和方法(3)_Python自学第二十二节
- 软件测试|Python删除列表元素的3种方法,你都会吗?
- Python count()方法:统计字符串出现的次数
- python实现线程池详解编程语言
- Python函数:一个简单的迭代详解编程语言
- Linux环境下安装Python(linux装python)
- Linux下运行Python的简单方法(linux运行.py)
- Python列表推导式的使用方法