I need to replace some characters as follows: & ➔ \\&, # ➔ \\#, ...
&
\\&
#
\\#
I coded as follows, but I guess there
How about this?
def replace_all(dict, str): for key in dict: str = str.replace(key, dict[key]) return str
then
print(replace_all({"&":"\&", "#":"\#"}, ""))
output
\&\#
similar to answer