以下是 Python字符串(str
)类全部方法的完整分类详解(基于Python 3.10+),包含每个方法的核心功能、参数说明和代码示例:
一、大小写操作
方法 | 功能 | 示例 |
---|
capitalize() | 首字母大写,其余小写 | "python".capitalize() → "Python" |
casefold() | 消除所有语言的大小写差异(强小写转换) | "Straße".casefold() → "strasse" |
lower() | 转为全小写 | "HELLO".lower() → "hello" |
upper() | 转为全大写 | "hello".upper() → "HELLO" |
swapcase() | 反转大小写 | "Hello".swapcase() → "hELLO" |
title() | 每个单词首字母大写 | "hello world".title() → "Hello World" |
二、格式调整
方法 | 功能 | 示例 |
---|
center(width[, fill]) | 居中填充到宽度width | "hi".center(5, '-') → "--hi-" |
ljust(width[, fill]) | 左对齐填充 | "hi".ljust(4, '*') → "hi**" |
rjust(width[, fill]) | 右对齐填充 | "42".rjust(5, '0') → "00042" |
zfill(width) | 左侧补零到指定宽度 | "-3".zfill(4) → "-003" |
expandtabs(tabsize=8) | 将\t 替换为空格(按tabsize 对齐) | "a\tb".expandtabs(4) → "a b" |
三、内容判断
方法 | 功能 | 示例 |
---|
startswith(prefix) | 是否以指定前缀开头 | "file.txt".startswith("file") → True |
endswith(suffix) | 是否以指定后缀结尾 | "image.png".endswith(".png") → True |
isalnum() | 是否全为字母或数字 | "abc123".isalnum() → True |
isalpha() | 是否全为字母 | "Python".isalpha() → True |
isdigit() | 是否全为数字(包括Unicode数字如² ) | "²34".isdigit() → False |
isdecimal() | 是否全为十进制数字(0-9 ) | "123".isdecimal() → True |
isnumeric() | 是否含数字字符(包括汉字数字如三 ) | "Ⅷ三".isnumeric() → True |
isspace() | 是否全为空白字符(空格、\t 等) | " \t".isspace() → True |
islower() | 是否全小写且至少一个字符 | "hello".islower() → True |
isupper() | 是否全大写且至少一个字符 | "HELLO".isupper() → True |
istitle() | 是否每个单词首字母大写 | "Hello World".istitle() → True |
isprintable() | 是否全为可打印字符(非控制字符) | "\n".isprintable() → False |
四、查找与替换
方法 | 功能 | 示例 |
---|
find(sub[, start, end]) | 返回子串首次索引(未找到返回-1 ) | "apple".find("p") → 1 |
rfind(sub[, ...]) | 从右向左查找子串 | "apple".rfind("p") → 2 |
index(sub[, ...]) | 类似find() 但未找到抛出ValueError | "apple".index("z") → ValueError |
rindex(sub[, ...]) | 从右向左查找并抛异常 | "apple".rindex("z") → ValueError |
count(sub[, start, end]) | 统计子串出现次数 | "banana".count("a") → 3 |
replace(old, new[, cnt]) | 替换子串(最多cnt 次) | "hello".replace("l", "x", 1) → "hexlo" |
removeprefix(prefix) | 移除前缀(Python 3.9+) | "http://xxx".removeprefix("http://") |
removesuffix(suffix) | 移除后缀(Python 3.9+) | "file.txt".removesuffix(".txt") |
五、拆分与连接
方法 | 功能 | 示例 |
---|
split([sep[, maxsplit]]) | 按分隔符拆分(默认按空格分割) | "a,b,c".split(",") → ["a", "b", "c"] |
rsplit([...]) | 从右向左拆分 | "a-b-c".rsplit("-", 1) → ["a-b", "c"] |
splitlines([keepends]) | 按换行符拆分(可选保留换行符) | "line1\nline2".splitlines() → ["line1", "line2"] |
join(iterable) | 连接可迭代对象(元素需为字符串) | "-".join(["2023", "08", "01"]) → "2023-08-01" |
partition(sep) | 分割为(前缀, sep, 后缀) 三元组 | "hello".partition("e") → ("h", "e", "llo") |
rpartition(sep) | 从右向左分割 | "hello".rpartition("l") → ("hel", "l", "o") |
六、修剪与去空格
方法 | 功能 | 示例 |
---|
strip([chars]) | 移除两侧指定字符(默认空格) | " text ".strip() → "text" |
lstrip([chars]) | 移除左侧指定字符 | "xxxtest".lstrip("x") → "test" |
rstrip([chars]) | 移除右侧指定字符 | "testxxx".rstrip("x") → "test" |
七、编码与转换
方法 | 功能 | 示例 |
---|
encode(encoding=...) | 转为字节序列(默认UTF-8) | "中文".encode("gbk") → b'\xd6\xd0\xce\xc4' |
format(*args, **kwargs) | 格式化字符串(支持复杂模板) | "{} + {} = {}".format(1, 2, 3) → "1 + 2 = 3" |
format_map(dict) | 通过字典格式化 | "{name} is {age}".format_map({"name": "Bob", "age": 20}) |
maketrans(x[, y, z]) | 创建字符映射表(配合translate() 使用) | `table = str.m |