字符串相关链接
类型 | 细分与链接 | 注释 |
字面值 | 转义字符 | |
字符串 | 字符串基础方法 | 拼接, 索引, 路径, 长字符串分行等等 |
内置类型str | 见下表 | |
format % values | ||
标准库string | 字符串常量 | 字符串模块中定义的常量 |
class string.Formatter | 自定义字符串格式化 | |
class string.Template(template) | 模板字符串 | |
str.format() | ||
标准库re | ||
模块内容 | ||
(常见)正则表达式例子 | ||
第三方库regex | regex | 更多功能和Unicode支持 |
标准库difflib | difflib | 计算差异的辅助工具 |
标准库textwarp | textwarp | 文本自动换行与填充 |
标准库stringprep | stringprep | 因特网字符串预备 |
标准库readline | readline | GNU readline 接口 |
标准库rlcompleter | rlcompleter | GNU readline 的补全函数 |
字符串的方法(更新于 2024.7.24)
我感觉比较重要的用红色标出来了
字符串方法(3.12) | 功能概述(详细内容点击我) |
str. capitalize () | 【3.8改】返回原字符串的副本,其首个字符大写,其余为小写 |
str. casefold () | 【3.3增】返回原字符串消除大小写的副本。 消除大小写的字符串可用于忽略大小写的匹配。 大小写折叠类似于小写,但更具攻击性,因为它旨在删除字符串中的所有大小写区别。例如,德语字母 |
str. center (width[, fillchar]) | 返回一个长度为宽度的字符串的中心。填充使用指定的fillchar(默认值为ASCII空格)完成。如果width小于或等于 |
str. count (sub[, start[, end]]) | 返回子字符串sub在[start,end]范围内非重叠出现的次数。可选参数start和end被解释为切片表示法。 |
str. encode (encoding="utf-8", errors="strict") | 【3.1 增】【3.9 改】 1. 返回编码为 2. `errors` 控制如何处理编码错误。如果是 3. 出于性能原因,除非实际发生编码错误、启用Python开发模式或使用调试构建,否则不会检查errors值的有效性。 |
str. endswith (suffix[, start[, end]]) | 如果字符串以指定的后缀结尾,则返回 |
| 返回字符串的副本,其中所有的制表符('\t')会由8个或n个空格替换,具体取决于当前列位置和给定的制表符宽度。 |
str.find (sub[, start[, end]]) | 返回子字符串 sub 在 注意:只有当你需要知道sub的位置时,才应该使用 |
str. format (*args, **kwargs) | 执行字符串格式化操作。 详见:string — Common string operations — Python 3.12.4 documentation |
str. format_map (mapping) | 【3.2 增】类似于 |
| 类似于 find(),但在找不到子类时会引发 ValueError。 |
str. isalnum () | 如果字符串中的所有字符都是字母数字(alphanumeric),并且至少有一个字符,则返回 True ,否则返回 False 。如果下列之一返回 True ,则字符 c 是字母数字: c.isalpha() 、 c.isdecimal() 、 c.isdigit() 或 c.isnumeric() 。 |
str. isalpha () | 如果字符串中的所有字符都是字母,并且至少有一个字符,返回 True ,否则返回 False 。 |
str. isascii () | 【3.7 增】如果字符串为空或字符串中的所有字符都是 ASCII ,返回 |
str. isdecimal () | 如果字符串中的所有字符都是十进制字符且该字符串至少有一个字符,则返回 True , 否则返回 False 。 |
str. isdigit () | 如果字符串中的所有字符都是数字,并且至少有一个字符,返回 True ,否则返回 False 。 |
str. isidentifier () | 如果字符串是有效的标识符,返回 |
str. islower () | 如果字符串中至少有一个区分大小写的字符且此类字符均为小写则返回 |
str. isnumeric () | 如果字符串中至少有一个字符且所有字符均为数值字符则返回 True ,否则返回 False 。 |
str. isprintable () | 如果字符串中的所有字符都是可打印的或字符串为空,则返回 |
str. isspace () | 如果字符串中只有空白字符且至少有一个字符则返回 True ,否则返回 False 。 |
str. istitle () | 如果字符串中至少有一个字符且为标题字符串则返回 |
str. isupper () | 如果字符串中至少有一个区分大小写的字符且此类字符均为大写则返回 |
str. join (iterable) | 返回一个由 iterable 中的字符串拼接而成的字符串。 如果 iterable 中存在任何非字符串值包括 bytes 对象则会引发 TypeError。 调用该方法的字符串将作为元素之间的分隔。 |
str. ljust (width[, fillchar]) | 返回字符串左对齐长度宽度的字符串。填充使用指定的fillchar(默认值为ASCII空格)完成。如果width小于或等于 |
str. lower () | 返回原字符串的副本,其所有区分大小写的字符均转换为小写。 |
str. lstrip ([chars]) | 返回删除前导字符的字符串副本。chars参数是一个字符串,指定要删除的字符集。如果省略或 |
static str. maketrans (x[, y[, z]]) | 此静态方法返回一个可供 str.translate() 使用的转换对照表。 如果只有一个参数,它必须是一个字典,将Unicode序数(整数)或字符(长度为1的字符串)映射到Unicode序数、字符串(任意长度)或 None 。字符键将被转换为序数。 如果有两个参数,它们必须是长度相等的字符串,在结果字典中,x中的每个字符将映射到y中相同位置的字符。如果有第三个参数,它必须是一个字符串,其字符将映射到结果中的 None 。 |
str. partition (sep) | 在 sep 首次出现的位置拆分字符串,返回一个 3 元组,其中包含分隔符之前的部分、分隔符本身,以及分隔符之后的部分。 |
str. removeprefix (prefix, /) | 【3.9 增】如果字符串以 前缀 字符串开头,返回 |
str. removesuffix (suffix, /) | 【3.9 增】如果字符串以 后缀 字符串结尾,并且 后缀 非空,返回 |
str. replace (old, new[, count]) | 返回字符串的副本,其中出现的所有子字符串 old 都将被替换为 new。 如果给出了可选参数 count,则只替换前 count 次出现。 |
str. rfind (sub[, start[, end]]) | 返回子字符串 sub 在字符串内被找到的最大(最右)索引,这样 sub 将包含在 |
str. rindex (sub[, start[, end]]) | 类似于 rfind(),但在子字符串 sub 未找到时会引发 ValueError。 |
str. rjust (width[, fillchar]) | 返回长度为 width 的字符串,原字符串在其中靠右对齐。 |
str. rpartition (sep) | 在 sep 最后一次出现的位置拆分字符串,返回一个 3 元组,其中包含分隔符之前的部分、分隔符本身,以及分隔符之后的部分。 如果分隔符未找到,则返回的 3 元组中包含两个空字符串以及字符串本身。 |
str. rsplit (sep=None, maxsplit=-1) | 返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串。 |
str. rstrip ([chars]) | 返回删除了尾随字符的字符串副本。chars参数是一个字符串,指定要删除的字符集。如果省略或 |
str. split (sep=None, maxsplit=-1) | 返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串。 |
str. splitlines ([keepends]) | 【3.2 改】返回由原字符串中各行组成的列表,在行边界的位置拆分。 结果列表中不包含行边界,除非给出了 keepends 且为真值。(请参考下边的 Representation & Desciption 表格) |
str. startswith (prefix[, start[, end]]) | 如果字符串以指定的 prefix 开始则返回 True ,否则返回 False 。 |
str. strip ([chars]) | 返回原字符串的副本,移除其中的前导和末尾字符。 |
str. swapcase () | 返回原字符串的副本,其中大写字符转换为小写,反之亦然。 |
str. title () | 返回原字符串的标题版本,其中每个单词第一个字母为大写,其余字母为小写。 该算法使用一个简单的独立于语言的单词定义为连续字母组。这个定义适用于许多情况,但它意味着缩写和所有格中的撇号形成了词的边界,这可能不是期望的结果 可以使用正则表达式构建撇号的解决方案 |
str. translate (table) | 返回原字符串的副本,其中每个字符按给定的转换表进行映射。 转换表必须是一个使用 __getitem__() 来实现索引操作的对象,通常为 mapping 或 sequence。 |
str. upper () | 返回原字符串的副本,其中所有区分大小写的字符均转换为大写。 |
str. zfill (width) | 返回一个字符串的副本,左填充ASCII |
Representation | Description(各种换行) |
---|---|
| Line Feed 换行 |
| Carriage Return 回车 |
| Carriage Return + Line Feed |
| Line Tabulation 行列表 |
| Form Feed 换页 |
| File Separator 文件分隔符 |
| Group Separator 组分隔符 |
| Record Separator 记录分隔符 |
| Next Line (C1 Control Code) |
| Line Separator 行分隔符 |
| Paragraph Separator 段落分隔符 |
看完了可以考别人的小问题
- str.find 和 str.index 有什么区别:index 没找到会报错 find 会返回 -1
- find, index, strip, partition, just, 都有从左往右的版本和从右往左的版本
- 'mississippi'.rstrip('ipz')返回什么:'mississ',因为会剥离 'ipz'的组合的后缀
- str.title, str.upper等等会改变原来的字符串吗:不会,返回的都是字符串副本