re模块 inputStr = 'hello 123 world 456' # replaceStr = inputStr.replace('111','222') # print(replaceStr) replaceStr = re.sub('\d+','222',inputStr) print(replaceStr) """ \d:匹配任意一个数字 包括[0-9]和其他数字字符 \D:(\d)的相反