# 如何去掉字符串中不需要的字符 s = ' abc 123 ' print(s.strip()) print(s.lstrip()) print(s.rstrip()) s = '----abc+++++' print(s.strip('-+')) s = 'abc:123' print(s[:3] + s[4:]) s = '\tabc\t123\txyz' print(s) print(s.replace('\t', '')) s = '\tabc\t123\txyz\ropq' import re print(re.sub('[\t\r]', '', s)) s = 'abc1230323xyz' # 一一对应替换字符 sMap = str.maketrans('abcxyz', 'xyzabc') print(sMap) print(s.translate(sMap))