Python 提供了多种字符串替换方法,以下是主要的方法和函数:
1. replace() 方法 - 最常用
# 基本用法
text = "Hello World"
new_text = text.replace("World", "Python")
print(new_text) # Hello Python
# 替换所有匹配项
text = "apple apple apple"
new_text = text.replace("apple", "orange")
print(new_text) # orange orange orange
# 指定替换次数
text = "banana banana banana"
new_text = text.replace("banana", "fruit", 2)
print(new_text) # fruit fruit banana2.re.sub() 方法 - 正则表达式替换
import re
# 基本正则替换
text = "My phone is 123-456-7890"
new_text = re.sub(r'\d{3}-\d{3}-\d{4}', 'XXX-XXX-XXXX', text)
print(new_text) # My phone is XXX-XXX-XXXX
# 使用函数进行替换
def to_upper(match):
return match.group().upper()
text = "hello world"
new_text = re.sub(r'\b\w+\b', to_upper, text)
print(new_text) # HELLO WORLD
# 替换并计数
text = "cat bat sat"
new_text, count = re.subn(r'at', 'AT', text)
print(f"新文本: {new_text}, 替换次数: {count}")
# 新文本: cAT bAT sAT, 替换次数: 33.translate() 方法 - 字符映射替换
# 创建转换表
trans_table = str.maketrans({
'a': '1',
'b': '2',
'c': '3'
})
text = "abc abc"
new_text = text.translate(trans_table)
print(new_text) # 123 123
# 删除特定字符
trans_table = str.maketrans('', '', 'aeiou') # 删除所有元音字母
text = "hello world"
new_text = text.translate(trans_table)
print(new_text) # hll wrld4.format() 和 f-string - 格式化替换
# format() 方法
template = "Hello, {name}! You are {age} years old."
new_text = template.format(name="Alice", age=25)
print(new_text) # Hello, Alice! You are 25 years old.
# f-string (Python 3.6+)
name = "Bob"
age = 30
new_text = f"Hello, {name}! You are {age} years old."
print(new_text) # Hello, Bob! You are 30 years old.5.模板字符串 - 更安全的替换
from string import Template
template = Template("Hello, $name! Your balance is $$$amount.")
new_text = template.substitute(name="Alice", amount=100)
print(new_text) # Hello, Alice! Your balance is $100.
# 安全替换,如果缺少变量不会报错
new_text = template.safe_substitute(name="Bob")
print(new_text) # Hello, Bob! Your balance is $amount.6.自定义替换函数
def multiple_replace(text, replacements):
"""同时进行多个替换"""
for old, new in replacements.items():
text = text.replace(old, new)
return text
text = "I love cats and dogs"
replacements = {
"cats": "kittens",
"dogs": "puppies",
"love": "adore"
}
new_text = multiple_replace(text, replacements)
print(new_text) # I adore kittens and puppies
# 使用正则表达式进行多个替换
def regex_multiple_replace(text, pattern_dict):
"""使用正则表达式进行多个替换"""
pattern = re.compile('|'.join(map(re.escape, pattern_dict.keys())))
return pattern.sub(lambda m: pattern_dict[m.group()], text)
text = "apple banana cherry"
pattern_dict = {
"apple": "fruit",
"banana": "yellow fruit",
"cherry": "red fruit"
}
new_text = regex_multiple_replace(text, pattern_dict)
print(new_text) # fruit yellow fruit red fruit总结对比
| 方法 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
replace() | 简单文本替换 | 简单直观,速度快 | 不支持正则,只能固定替换 |
re.sub() | 复杂模式匹配 | 功能强大,支持正则 | 性能稍差,语法复杂 |
translate() | 字符级别的替换 | 高效,适合批量字符替换 | 只能单字符映射 |
format()/f-string | 格式化字符串 | 易读,类型安全 | 不是真正的替换操作 |
Template | 安全模板替换 | 安全,防止注入攻击 | 功能有限 |
实际示例:清理文本
def clean_text(text):
"""清理文本中的特殊字符"""
# 替换多个空格为单个空格
text = re.sub(r'\s+', ' ', text)
# 移除特殊字符但保留常见标点
text = re.sub(r'[^\w\s.,!?-]', '', text)
# 替换常见错别字
replacements = {
'teh': 'the',
'adn': 'and',
'thier': 'their'
}
for old, new in replacements.items():
text = re.sub(r'\b' + old + r'\b', new, text)
return text.strip()
dirty_text = " teh quick brown fox adn thier friends! "
clean = clean_text(dirty_text)
print(clean) # the quick brown fox and their friends!选择哪种方法取决于具体需求:
简单替换用
replace()复杂模式用
re.sub()字符映射用
translate()格式化输出用
f-string模板安全用
Template