Python如何去掉字符串中不需要的字符

Jackey Python 1,615 次浏览 , 没有评论
# 如何去掉字符串中不需要的字符
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))

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Go