一、字符串基础特性
- 字符串是 不可变对象(immutable)
 
- 支持索引 
s[0]、切片 s[1:4]、遍历 for c in s 
- 支持 
+ 拼接、* 重复:"ab" * 3 → "ababab" 
1 2 3
   | s = "Hello" print(s[0])       print(s[::-1])   
   | 
 
二、常用字符串方法(按功能分类)
字符判断类(返回 True/False)
| 方法 | 
功能 | 
示例 | 
.isalpha() | 
是否全是字母 | 
"abc".isalpha() → True | 
.isdigit() | 
是否全是数字(0-9) | 
"123".isdigit() → True | 
.isalnum() | 
是否是字母或数字 | 
"a1".isalnum() → True | 
.isspace() | 
是否全是空白字符 | 
" \t\n".isspace() → True | 
.islower() | 
是否全是小写 | 
"hello".islower() → True | 
.isupper() | 
是否全是大写 | 
"HELLO".isupper() → True | 
.istitle() | 
是否是标题格式(首字母大写) | 
"Hello".istitle() → True | 
.startswith(prefix) | 
是否以某字符串开头 | 
"abc".startswith("a") → True | 
.endswith(suffix) | 
是否以某字符串结尾 | 
"file.txt".endswith(".txt") → True | 
⚠️ 注意:isdigit() 不识别负数、小数;-123 是 str 但 "-123".isdigit() 是 False
分割与合并
.split(sep=None, maxsplit=-1)
- 按分隔符分割字符串,返回列表
 
sep=None 时按空白(空格、制表符、换行)分割 
1 2 3
   | "hello world".split()            "a,b,c".split(",")               "a,b,c,d".split(",", maxsplit=2) 
   | 
 
.rsplit()
.join(iterable)
1 2
   | ", ".join(['a', 'b', 'c'])       "".join(map(str, [1,2,3]))       
   | 
 
.partition(sep)
1
   | "hello.txt".partition(".")       
  | 
 
.rpartition(sep)
查找与定位
| 方法 | 
功能 | 
找不到时返回 | 
.find(sub) | 
返回首次出现的索引 | 
-1 | 
.rfind(sub) | 
返回最后一次出现的索引 | 
-1 | 
.index(sub) | 
同 find,但找不到抛 ValueError | 
抛异常 | 
.rindex(sub) | 
从右边找,找不到也抛异常 | 
抛异常 | 
1 2 3 4
   | s = "hello world" s.find("o")       s.rfind("o")      s.find("xyz")    
   | 
 
替换与删除
.replace(old, new, count=-1)
1
   | "hello".replace("l", "x", 1)  
  | 
 
.strip([chars])
1 2
   | "  hello  ".strip()         "###hello###".strip("#")   
   | 
 
.lstrip([chars])
.rstrip([chars])
.removeprefix(prefix)
1
   | "hello.txt".removeprefix("hello")  
  | 
 
.removesuffix(suffix)
1
   | "hello.txt".removesuffix(".txt")  
  | 
 
大小写转换
| 方法 | 
功能 | 
.lower() | 
全转小写 | 
.upper() | 
全转大写 | 
.title() | 
每个单词首字母大写 | 
.capitalize() | 
整个字符串首字母大写 | 
.swapcase() | 
大小写互换 | 
1 2 3 4 5
   | "Hello World".lower()         "hello".upper()               "hello world".title()         "hello".capitalize()          "Hello".swapcase()           
   | 
 
填充与对齐
| 方法 | 
功能 | 
.center(width, fillchar=' ') | 
居中,总长 width | 
.ljust(width, fillchar) | 
左对齐 | 
.rjust(width, fillchar) | 
右对齐 | 
.zfill(width) | 
左补0(常用于数字格式) | 
1 2
   | "42".center(10, '-')     "42".zfill(5)           
   | 
 
编码与字节转换
| 方法 | 
功能 | 
.encode(encoding='utf-8') | 
转为字节串 | 
.decode() | 
字节串转字符串(在 bytes 对象上) | 
1 2
   | "你好".encode('utf-8')      b'\xe4\xbd\xa0'.decode()    
  | 
 
其他实用方法
| 方法 | 
功能 | 
.count(sub) | 
子串出现次数 | 
.isspace() | 
是否全是空白 | 
.isnumeric() | 
是否是数字(比 isdigit 更广,支持中文数字) | 
.isdecimal() | 
是否是十进制数字(最严格) | 
1 2 3 4 5
   | "123".isnumeric()      "①".isnumeric()        "½".isnumeric()        "123".isdecimal()      "①".isdecimal()       
   | 
 
三、字符串格式化(4种方式)
1. % 格式化(旧式,不推荐)
1
   | "name: %s, age: %d" % ("Alice", 25)
  | 
 
1 2
   | "Hello {name}, you are {age} years old".format(name="Alice", age=25) "{0} {1} {0}".format("a", "b")  
  | 
 
3. f-string(Python 3.6+,最推荐 ✅)
1 2 3 4 5
   | name = "Alice" age = 25 f"Hello {name}, you are {age} years old" f"2+2={2+2}"   f"{name:>10}" 
   | 
 
4. Template(安全插值,适合用户输入)
1 2 3
   | from string import Template t = Template("Hello $name") t.substitute(name="Alice")
   | 
 
四、高级技巧与最佳实践
1. 安全判断字符串是否“可转为数字”
1 2 3 4 5 6
   | def is_number(s):     try:         float(s)         return True     except ValueError:         return False
   | 
 
支持:"123", "-12.3", "inf", "NaN"
2. 多行字符串(三引号)
1 2 3 4 5
   | text = """ This is a multi-line string.     Preserves indentation. """
   | 
 
3. 原始字符串(r""),避免转义
1 2
   | path = r"C:\new\folder\test.txt"   regex = r"\d+"  
   | 
 
4. 字符串拼接性能对比
| 方式 | 
推荐场景 | 
" ".join(list) | 
多字符串拼接(高效)✅ | 
f"{a}{b}{c}" | 
少量变量拼接 ✅ | 
+ 拼接 | 
简单场景,但大量拼接慢 ❌ | 
io.StringIO | 
极大量动态拼接(日志等) | 
1 2 3 4 5 6 7
   |  result = "".join([s1, s2, s3])
 
  s = "" for i in range(10000):     s += str(i)  
 
  | 
 
5. 使用正则表达式(re 模块)
1 2 3 4 5
   | import re
  re.findall(r'\d+', "a12b34")      re.sub(r'\s+', ' ', "a  b")       re.match(r'^\d+$', "123")        
   | 
 
6. 字符串不可变性 → 修改需新建
1 2 3
   | s = "hello"
  s = 'H' + s[1:]  
   | 
 
五、常见陷阱与注意事项
| 问题 | 
说明 | 
isinstance(s, str) | 
比 type(s) == str 更安全 | 
str.isdigit() 不识别负数 | 
"-" 不是数字字符 | 
| 中文、Unicode 支持良好 | 
Python 3 默认 UTF-8 | 
字符串比较用 ==,不是 is | 
"abc" == "abc" ✅,"abc" is "abc" ❌(可能缓存) | 
六、实用工具函数示例
1 2 3 4 5 6 7 8 9 10 11 12 13
   |  def is_palindrome(s):     s = ''.join(filter(str.isalnum, s.lower()))     return s == s[::-1]
 
  def extract_digits(s):     return ''.join(filter(str.isdigit, s))
 
  def camel_to_snake(s):     import re     return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', s).lower()
 
  |