用法 | 说明 |
---|---|
str.replace(old, new[, max]) | replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。 |
re.sub(’\d+’,‘222’,inputStr) | 将inputStr中的数字替换成222 |
- \d:匹配任意一个数字 包括[0-9]和其他数字字符
- \D:(\d)的相反
import re
inputStr = 'hello 1111 world 456'
replaceStr = inputStr.replace('111','222')
print(replaceStr)
replaceStr = re.sub('\d+','222',inputStr)
print(replaceStr)
replaceStr = re.sub('\D+','----',inputStr)
print(replaceStr)
来源:CSDN
作者:weixin_43384009
链接:https://blog.csdn.net/weixin_43384009/article/details/103801047