【Python】杂乱-[代码]Python 替换字符串中相关字符的方法
字符串替换
- 说明
常规的字符替换方式。 - 代码
text = "Hello,world!"
new_text = text.replace(","," ")
print("text:" , text)
print("new_text:" , new_text)
- 结果
- 说明
替换多个不同的字符。 - 代码
text = "one two one two one"
translation_table = str.maketrans({'o': 'O', 't': 'T'})
new_text = text.translate(translation_table)
print(new_text)
- 结果