| 方法 | 描述 |
|---|---|
| str.capitalize() | 返回副本,首字母大写,其余小写 |
| str.upper() | 全部大写 |
| str.isupper() | 是否都是大写 |
| str.lower() | 小写 |
| str.islower() | 是否都是小写 |
| str.swapcase() | 转换大小写 |
| str.title() | 首字母大写,其余小写 |
| str.istitle() | |
| str.center(width[, fillchar]) | 返回长度为width的字符串,并使得自身居中。 |
| str.zfill(width) | 填充0 |
| str.ljust(width[, fillchar]) | |
| str.rjust(width[, fillchar]) | |
| str.lstrip([chars]) | |
| str.rstrip([chars]) | |
| str.strip([chars]) | 去除空白符 |
| str.find(sub[, start[, end]]) | 返回sub字符串的索引 |
| str.index(sub[, start[, end]]) | 未找到则报错 |
| str.rfind(sub[, start[, end]]) | 从左边找 |
| str.rindex(sub[, start[, end]]) | |
| str.expandtabs(tabsize=8) | 返回副本,空格替换制表符 |
| str.replace(old, new[, count]) | 替换字符,count决定了替换几个 |
| str.partition(sep) | 分割字符串,并返回分割时用到的字符 |
| str.rpartition(sep) | |
| str.split(sep=None, maxsplit=-1) | 分割字符串 |
| str.rsplit(sep=None, maxsplit=-1) | |
| str.splitlines([keepends]) | 分割行 |
| str.startswith(prefix[, start[, end]]) | 是否以prefix开头 |
| str.endswith(suffix[, start[, end]]) | 如果字符串以指定的suffix结尾则返回False,否则返回True。 |
| str.isalnum() | 字符串是字母和数字,则返回true |
| str.isalpha() | 字符串都是字母,则返回true |
| str.isdecimal() | 字符串都是十进制数,则返回true |
| str.isdigit() | 字符串都是数字,则返回true |
| str.isnumeric() | |
| str.isspace() | 只有空格字符 |
| str.join(iterable) | 连接列表 |
| str.encode(encoding=“utf-8”, errors=“strict”) | 将字符串的编码版本作为字节对象返回。 |
| str.count(sub[, start[, end]]) | 返回在[start, end]范围内的子串sub非重叠出现的次数 |
| str.format(*args, **kwargs) | 格式化 |
| str.format_map(mapping) | 格式化 |
| str.translate(table) | 按照table来翻译 |
| in | 判断字符串是否存在另一个字符串里,类似于contain |
| not in | 判断字符串是否存在另一个字符串里,类似于contain |
参考文献:
https://docs.python.org/3/library/stdtypes.html#string-methods
来源:CSDN
作者:Claroja
链接:https://blog.csdn.net/claroja/article/details/103697816