python之字符串替换
方法一:
""" str.replace(old, new[, max]) old -- 将被替换的子字符串。 new -- 新字符串,用于替换old子字符串。 max -- 可选字符串, 替换不超过 max 次 """ temp_str = 'this is a test' print(temp_str.replace(temp_str[2],'aa')) print(temp_str.replace(temp_str[:2],'aa')) >>> thaas aas a test aais is a test
方式二:
在字符串中替换自己想要的字符串
# -*- coding=utf-8 -*- s = "{a}bc" s=s.format(a="123") print(s) >>>123bc