python multiple character is replaced by a character

Want to start () replaced with replace, but to deal with more characters to write code is not clean

a='fs233*bb>>c##ad'
a.replace('*','-').replace('>','-').replace('#','-')
print(a)
fs233bbcad

You can only replace a character string or
later discovered can be used re.sub () perfect solution

import re
a='fs233*bb>>c##ad'
re.sub(r'[\*>#]','-',a)
print(a)
fs233bbcad

The re.sub first parameter () is a pattern, a regular expression, so the example [*> #] represents [any character], i.e. the replacement of a *> # is -. Also learn more please use regular expressions

Published 11 original articles · won praise 3 · Views 2743

Guess you like

Origin blog.csdn.net/qq_29869111/article/details/102623640