re模块替换字符串中的某些字符

>>> import re # 导入re模块
>>> s1 = '111121265456456454111' # 定义字符串s1
>>> s1
'111121265456456454111'
>>> s2 = re.sub(r'1','6',s1) # 将s1中的字符1替换为6
>>> s2
'666626265456456454666'
>>> s2
'666626265456456454666'
>>> s3 = re.sub('6','8',s2) # 将s2中的6替换成8
>>> s3
'888828285458458454888'

猜你喜欢

转载自blog.csdn.net/xuezhangjun0121/article/details/78470471
今日推荐