Python 字符串替换 HenryZ 2020-10-31 更新于 2022-07-05 目录 查找固定字符串并替换 正则表达式查找并替换 查找固定字符串并替换使用字符串对象的 .replace() 方法 s = 'a---c' s.replace('-','') # 'ac' s.replace('--','-') # 'a--c' 正则表达式查找并替换使用 re.sub() import re s = 'a---c' re.sub(r'[-]+', '-', s) #'a-c' Please enable JavaScript to view the comments powered by giscus.