Python:字符串方法手册

在 Python 中,字符串(str)是最常用的数据类型之一,用于表示文本信息。Python 的字符串对象不仅支持索引、切片和拼接,还提供了丰富的方法来处理大小写、查找替换、分割合并、对齐填充、编码转换等操作。

一、大小写转换

lower()

将字符串中所有字母转换为小写。

upper()

将字符串中所有字母转换为大写。

print("mediaTEA".lower())   # mediateaprint("mediaTEA".upper())   # MEDIATEA

capitalize()

首字母大写,其余小写。

title()

每个单词的首字母大写。

print("hello world".capitalize())  # Hello worldprint("hello world".title())  # Hello World

swapcase()

大小写互换。

print("mediaTEA".swapcase())  # MEDIAtea

casefold()

更彻底的小写转换,主要用于文本比较时的统一处理,强于 lower()。

print("Straße".casefold())  # strasseprint("Python".casefold() == "PYTHON".casefold())  # True

说明:Unicode 标准中的大小写规约方法,适用于国际化应用中需要“忽略大小写”的场合。

二、查找与统计

find(sub[, start[, end]])

返回子串 sub 在字符串中第一次出现的位置,找不到返回 -1。

参数:

sub:要查找的子串

start(可选):搜索起始位置

end(可选):搜索结束位置(不含)

返回值:索引(int),未找到返回 -1。

s = "banana"print(s.find("na"))     # 2print(s.find("na", 3))  # 4print(s.find("xy"))     # -1

rfind(sub[, start[, end]])

从右侧开始查找 sub,返回最后一次出现的位置,未找到返回 -1。

index(sub[, start[, end]])

与 find 相同,但未找到时抛 ValueError。

rindex(sub[, start[, end]])

与 rfind 相同,但未找到时抛 ValueError。

print("banana".rfind("na"))  # 4print("banana".index("na"))  # 2print("banana".rindex("na"))  # 4

count(sub[, start[, end]])

统计子串 sub 出现的次数。

print("banana".count("na"))  # 2

三、拆分与连接

split(sep=None, maxsplit=-1)

按指定分隔符切割字符串,返回列表。

参数:

sep:分隔符,默认任意空白

maxsplit:最大分割次数,默认无限制

返回值:列表。

s = "a b c"print(s.split())        # ['a', 'b', 'c']print(s.split(" ", 1))  # ['a', 'b c']

rsplit(sep=None, maxsplit=-1)

从右侧开始分割,可指定最大分割次数。

print("a b c".rsplit(" ", 1))  # ['a b', 'c']

splitlines([keepends])

按行切分。

参数:

keepends(可选):默认为 False,表示不保留换行符。若为 True,则保留换行符。

s = "a\nb\nc"print(s.splitlines())        # ['a', 'b', 'c']print(s.splitlines(True))    # ['a\n', 'b\n', 'c']

partition(sep)

从左侧开始基于指定分隔符将字符串分为三部分:首、分隔符、末。

rpartition(sep)

从右侧开始基于指定分隔符将字符串分为三部分:首、分隔符、末。

print("hello mediaTEA world".partition(" "))  # ('hello', ' ', 'mediaTEA world')print("hello mediaTEA world".rpartition(" ")) # ('hello mediaTEA', ' ', 'world')

join(iterable)

将可迭代对象中的多个字符串连接为一个新的字符串,连接时以调用者作为分隔符。

参数:

iterable:可迭代对象(如列表、元组),其中的元素必须全部是字符串,否则会抛出 TypeError。

返回值:

拼接后的新字符串。

words = ["a", "b", "c"]print("-".join(words))   # a-b-c

四、判断字符串内容

这些方法通常返回布尔值,用于判断字符串是否满足某种字符条件。

isalpha()

是否全部为字母。

isalnum()

是否全部由字母和数字组成。

isdigit()

是否全部为数字。

isdecimal()

是否只包含“十进制数字字符”(更严格,常用于纯阿拉伯数字检查)。

isnumeric()

是否只包含数字字符(包括中文数字等)。

isspace()

是否只包含空白字符。

print("abc".isalpha())   # Trueprint("abc123".isalnum())   # Trueprint("123".isdigit())      # Trueprint("123".isdecimal())  # True(全角十进制)print("一二三".isnumeric())  # Trueprint("   ".isspace())      # True

islower() 

是否全小写。

isupper() 

是否全大写。

istitle() 

是否是标题格式(每个单词首字母大写)。

print("hello".islower())       # Trueprint("HELLO".isupper())       # Trueprint("Hello World".istitle()) # True

isascii() 

是否全为 ASCII 字符。(3.7+)

print("abc".isascii())   # Trueprint("你好".isascii())  # False

isidentifier()

是否是合法的 Python 标识符(可作为变量名)。

print("user_name".isidentifier()) # Trueprint("3name".isidentifier()) # False

isprintable()

是否都是可打印字符(空格也算可打印),不可打印的包括控制符(如换行 \n、制表 \t 等)。

print("Hello".isprintable())    # Trueprint("Hello\nWorld".isprintable())  # False

startswith(prefix[, start[, end]])

判断字符串是否以指定前缀开头。

endswith(suffix[, start[, end]])

判断字符串是否以指定后缀结尾。

print("hello".startswith("he"))  # Trueprint("hello".endswith("lo"))    # True

五、对齐与填充

ljust(width[, fillchar])

左对齐,右侧空位填充空格或指定字符。

参数:

width:总宽度

fillchar(可选):填充字符,默认空格。

rjust(width[, fillchar])

右对齐,左侧空位填充空格或指定字符。

center(width[, fillchar])

居中对齐,两侧空位填充空格或指定字符。

print("hi".ljust(5, "-"))  # hi---print("hi".rjust(5, "-"))   # ---hiprint("hi".center(5, "-"))  # --hi-

zfill(width)

左侧填充 0,直到达到指定宽度。

print("42".zfill(5))  # 00042

六、替换与删除

replace(old, new[, count])

替换子串。

参数:

old:要替换的子串

new:新子串

count(可选):替换次数,默认全替换

返回值:新字符串。

s = "apple apple"print(s.replace("apple", "orange"))      # orange orangeprint(s.replace("apple", "orange", 1))   # orange apple

expandtabs(tabsize=8)

将字符串中的制表符 \t 展开为空格,默认每个 tab 占 8 个空格位,可通过参数设置宽度。

print("abc\tdef".expandtabs())       # 'abc     def'print("abc\tdef".expandtabs(4))     # 'abc def'

format(*args, **kwargs)

使用花括号 {} 进行位置参数或关键字参数的格式化替换,适合构造复杂字符串。

print("Hello, {}!".format("world"))              # 'Hello, world!'print("{0} + {0} = {1}".format("a", "2a"))        # 'a + a = 2a'print("{name} is {age} years old".format(name="mediaTEA", age=7))    # mediaTEA is 7 years old

说明:

*args:位置参数,可通过 {0}、{1} 等引用。

**kwargs:关键字参数,可通过 {关键字} 引用。

format_map(mapping)

与 format() 类似,但只支持使用字典(或任意 mapping 对象)进行替换。

data = {"name": "Alice", "age": 18}print("{name} is {age}".format_map(data))  # Alice is 18

strip([chars])

去除字符串两端的空白或指定字符。

参数:

chars(可选):要去除的字符集合(注意不是子串)。

print(" hello ".strip())      # "hello"print("..hi..".strip("."))    # "hi"

lstrip([chars])

去除左侧空白或指定字符。

rstrip([chars])

去除右侧空白或指定字符。

print("xxhelloxx".lstrip("x"))  # "helloxx"print("xxhelloxx".rstrip("x"))  # "xxhello"

removeprefix(prefix) 

(Python 3.9+)若字符串以指定前缀开头,则移除该前缀;否则原样返回。

removesuffix(suffix) 

(Python 3.9+)若字符串以指定后缀结尾,则移除该后缀;否则原样返回。

print("unhappy".removeprefix("un"))     # happyprint("data.csv".removesuffix(".csv"))  # dataprint("data.csv".removesuffix(".txt"))  # data.csv

七、编码与解码

encode(encoding="utf-8", errors="strict")

将字符串编码为字节串(bytes),常用于文本转存、网络传输等。

参数:

encoding:可选。编码方式,如 'utf-8'、'gbk'、'ascii' 等,默认为 'utf-8'。

errors:可选。出错处理方式。常见取值有:

'strict':默认,遇到不能编码的字符报错。

'ignore':忽略无法编码的字符。

'replace':用 '?' 或 U+FFFD 替换非法字符。

返回值:bytes 类型的字节串。

print("你好".encode())              # b'\xe4\xbd\xa0\xe5\xa5\xbd'print("abc".encode('ascii'))        # b'abc'print("你好".encode('ascii', errors='ignore'))  # b''

bytes.decode(encoding="utf-8", errors="strict")

将字节串解码为字符串。

b = b'\xe4\xbd\xa0\xe5\xa5\xbd'print(b.decode("utf-8"))  # 你好

八、其他方法

str.maketrans(mapping) 

根据映射规则生成一个转换表。(字符串类方法)

translate(table)

根据 maketrans 生成的转换表,批量替换或删除字符。比多次 replace() 更高效。

# 1. 用两个字符串建立映射规则table = str.maketrans("abc", "123")print("abc cab".translate(table))  # 输出:123 321
# 2. 用字典建立映射规则table = str.maketrans({"-": "", "_": " "})  # 删除“-”,将“_”替换为空格print("data-set_v1".translate(table))  # 输出:dataset v1
# 删除字符用 None:table = str.maketrans({"a": None})print("banana".translate(table))  # bnn   (删除所有 a)

📘 小结

大小写转换:lower()、upper()、swapcase() 等

查找与统计:find()、index()、count() 等

拆分与连接:split()、splitlines()、partition()、join() 等

判断字符串内容:isalpha()、isdigit()、isdecimal()、isidentifier()、isprintable() 等

对齐与填充:ljust()、rjust()、center()、zfill() 等

替换与删除:replace()、format()、strip()、rstrip()、removeprefix() 等

编码与解码:encode() / decode()

其他方法:maketrans()、translate()

图片

“点赞有美意,赞赏是鼓励”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值