Python 内置字符串操作函数

以下是 Python 内置字符串操作函数及相关方法的表格整理,涵盖常用功能、简介及示例:

函数名 简介 示例
upper() 将字符串所有字符转换为大写。 "Hello World".upper() → "HELLO WORLD"
lower() 将字符串所有字符转换为小写。 "HELLO WORLD".lower() → "hello world"
capitalize() 首字母大写,其余小写。 "hello world".capitalize() → "Hello world"
strip() 去除字符串两端空白符(空格、换行、制表符等)。 " Hello ".strip() → "Hello"
lstrip() 去除字符串左侧空白符。 " Hello ".lstrip() → "Hello "
rstrip() 去除字符串右侧空白符。 " Hello ".rstrip() → " Hello"
split(sep=None) 按分隔符 sep 分割字符串为列表,默认按空格分割。 "a,b,c".split(',') → ['a', 'b', 'c']
join(iterable) 用字符串连接可迭代对象的元素。 ' '.join(["Hello", "World"]) → "Hello World"
find(sub) 返回子串 sub 首次出现的索引,未找到返回 -1 "apple".find("p") → 1
index(sub) 返回子串 sub 首次出现的索引,未找到抛出 ValueError "apple".index("p") → 1
replace(old, new) 替换子串 oldnew,可指定替换次数。 "banana".replace("a", "x", 1) → "bxnana"
startswith(prefix) 检查字符串是否以 prefix 开头。返回布尔值。 "Hello".startswith("H") → True
endswith(suffix) 检查字符串是否以 suffix 结尾。返回布尔值。 "Hello".endswith("o") → True
count(sub) 统计子串 sub 在字符串中出现的次数。 "banana".count("a") → 3
isdigit() 检查字符串是否全为数字字符。返回布尔值。 "123".isdigit() → True
isalpha() 检查字符串是否全为字母字符。返回布尔值。 "Alpha".isalpha() → True
isalnum() 检查字符串是否全为字母或数字字符。返回布尔值。 "A1B2".isalnum() → True
isspace() 检查字符串是否全为空白符(空格、换行、制表符等)。返回布尔值。 " ".isspace() → True
len() 返回字符串的长度。 len("Hello") → 5
format() 格式化字符串,通过 {} 和格式说明符控制输出。 "{name} is {age}".format(name="Alice", age=30) → "Alice is 30"
f-string 字符串格式化语法,直接嵌入表达式。 name = "Alice"; age = 30 → f"{name} is {age}" → "Alice is 30"
translate(table) 使用翻译表 table 替换字符(需配合 str.maketrans())。 trans = str.maketrans({'a': 'x'}) → "cat".translate(trans) → "ctx"
repeat(n) 重复字符串 n 次。 "Hi ".repeat(3) → "Hi Hi Hi "
center(width) 字符串居中,总长度为 width,两侧用空格填充。 "test".center(10) → " test "
expandtabs(tabsize) 将制表符 \t 转换为指定长度的空格(默认8)。 "\tHello".expandtabs(4) → " Hello"
casefold() 类似 lower(),但更彻底(如德语 ßss)。 "Straße".casefold() → "straße"
swapcase() 反转字符串中的大小写。 "Hello World".swapcase() → "hELLO wORLD"
partition(sep) 将字符串按 sep 分割为 (前缀, sep, 后缀) 三元组。 "applepie".partition("pie") → ("apple", "pie", "")
rpartition(sep) 反向分割字符串,返回 (前缀, sep, 后缀) "pieapple".rpartition("pie") → ("", "pie", "apple")
zfill(width) 左侧补零,使字符串总长度为 width "123".zfill(5) → "00123"
ljust(width) 左对齐,右侧用空格填充至 width 长度。 "abc".ljust(5) → "abc "
rjust(width) 右对齐,左侧用空格填充至 width 长度。 "abc".rjust(5) → " abc"
splitlines() 按换行符分割字符串为列表。 "Line1\nLine2".splitlines() → ["Line1", "Line2"]
title() 每个单词首字母大写,其余小写(忽略非字母字符)。 "hello world!".title() → "Hello World!"
isupper() 检查字符串是否全为大写字符。返回布尔值。 "HELLO".isupper() → True
islower() 检查字符串是否全为小写字符。返回布尔值。 "hello".islower() → True
istitle() 检查字符串是否为标题格式(每个单词首字母大写)。 "Hello World".istitle() → True
isdecimal() 检查字符串是否全为十进制数字字符。返回布尔值。 "123.45".isdecimal() → False(含小数点)
isidentifier() 检查字符串是否为合法的 Python 标识符。 "var_1".isidentifier() → True

说明:

  1. 格式化方法format() 和 f-string 是 Python 中最灵活的字符串格式化方式,支持变量嵌入、类型转换等。
  2. 大小写处理casefold()lower() 更国际化,适用于多语言场景。
  3. 分割与连接split()join() 常用于数据处理,如 CSV 解析。
  4. 翻译表translate() 需搭配 str.maketrans() 使用,适用于批量字符替换。
  5. 边界检查startswith()endswith() 常用于验证输入格式(如文件扩展名)。
  6. 填充与对齐zfill()ljust()rjust() 在数据格式化(如对齐表格)时非常有用。

此表格覆盖了 Python 字符串操作的核心功能,适用于快速查阅和日常开发。

posted @ 2025-03-18 17:08  nxhujiee  阅读(213)  评论(0)    收藏  举报