# What’s the most efficient approach to replace many characters in a string?

## Problem

I need to replace the following characters: # #, & &, & &, & &, & &, & &, & &, & &, &

I coded as follows, but I’m sure there’s a better way to do it. Are there any hints?

``````strs = strs.replace('&', '\&')
strs = strs.replace('#', '\#')
...
``````

## Solution #1

I timed all of the methods in the existing answers, as well as one additional approach.

The quickest approach to replace & -> & and # -> # in an abc&def#ghi input string was to chain together the replacements like this: text. ‘&’,’&’,’&’,’&’,’&’,’&’,’&’,’& replace(‘#’,’#’),’#’,’#’,’#’,’#’,’#’,’

Timings for each function are as follows:

The following are the functions:

``````def a(text):
chars = "&#"
for c in chars:
text = text.replace(c, "\\" + c)

def b(text):
for ch in ['&','#']:
if ch in text:
text = text.replace(ch,"\\"+ch)

import re
def c(text):
rx = re.compile('([&#])')
text = rx.sub(r'\\\1', text)

RX = re.compile('([&#])')
def d(text):
text = RX.sub(r'\\\1', text)

def mk_esc(esc_chars):
return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
esc = mk_esc('&#')
def e(text):
esc(text)

def f(text):
text = text.replace('&', '\&').replace('#', '\#')

def g(text):
replacements = {"&": "\&", "#": "\#"}
text = "".join([replacements.get(c, c) for c in text])

def h(text):
text = text.replace('&', r'\&')
text = text.replace('#', r'\#')

def i(text):
text = text.replace('&', r'\&').replace('#', r'\#')
``````

Timed like this:

``````python -mtimeit -s"import time_functions" "time_functions.a('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.b('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.c('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.d('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.e('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.f('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.g('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.h('abc&def#ghi')"
python -mtimeit -s"import time_functions" "time_functions.i('abc&def#ghi')"
``````

Here’s some code that does the same thing but with extra escape characters (‘* >#+-.!\$):

``````def a(text):
chars = "\\`*_{}[]()>#+-.!\$"
for c in chars:
text = text.replace(c, "\\" + c)

def b(text):
for ch in ['\\','`','*','_','{','}','[',']','(',')','>','#','+','-','.','!','\$','\'']:
if ch in text:
text = text.replace(ch,"\\"+ch)

import re
def c(text):
rx = re.compile('([&#])')
text = rx.sub(r'\\\1', text)

RX = re.compile('([\\`*_{}[]()>#+-.!\$])')
def d(text):
text = RX.sub(r'\\\1', text)

def mk_esc(esc_chars):
return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
esc = mk_esc('\\`*_{}[]()>#+-.!\$')
def e(text):
esc(text)

def f(text):
text = text.replace('\\', '\\\\').replace('`', '\`').replace('*', '\*').replace('_', '\_').replace('{', '\{').replace('}', '\}').replace('[', '\[').replace(']', '\]').replace('(', '\(').replace(')', '\)').replace('>', '\>').replace('#', '\#').replace('+', '\+').replace('-', '\-').replace('.', '\.').replace('!', '\!').replace('\$', '\\$')

def g(text):
replacements = {
"\\": "\\\\",
"`": "\`",
"*": "\*",
"_": "\_",
"{": "\{",
"}": "\}",
"[": "\[",
"]": "\]",
"(": "\(",
")": "\)",
">": "\>",
"#": "\#",
"+": "\+",
"-": "\-",
".": "\.",
"!": "\!",
"\$": "\\$",
}
text = "".join([replacements.get(c, c) for c in text])

def h(text):
text = text.replace('\\', r'\\')
text = text.replace('`', r'\`')
text = text.replace('*', r'\*')
text = text.replace('_', r'\_')
text = text.replace('{', r'\{')
text = text.replace('}', r'\}')
text = text.replace('[', r'\[')
text = text.replace(']', r'\]')
text = text.replace('(', r'\(')
text = text.replace(')', r'\)')
text = text.replace('>', r'\>')
text = text.replace('#', r'\#')
text = text.replace('+', r'\+')
text = text.replace('-', r'\-')
text = text.replace('.', r'\.')
text = text.replace('!', r'\!')
text = text.replace('\$', r'\\$')

def i(text):
text = text.replace('\\', r'\\').replace('`', r'\`').replace('*', r'\*').replace('_', r'\_').replace('{', r'\{').replace('}', r'\}').replace('[', r'\[').replace(']', r'\]').replace('(', r'\(').replace(')', r'\)').replace('>', r'\>').replace('#', r'\#').replace('+', r'\+').replace('-', r'\-').replace('.', r'\.').replace('!', r'\!').replace('\$', r'\\$')
``````

Here are the results for the same abc&def#ghi input string:

And with a longer input string (## *Something* and [another] thing in a longer sentence with {more} things to replace\$):

Adding a few of different options

``````def ab(text):
for ch in ['\\','`','*','_','{','}','[',']','(',')','>','#','+','-','.','!','\$','\'']:
text = text.replace(ch,"\\"+ch)

def ba(text):
chars = "\\`*_{}[]()>#+-.!\$"
for c in chars:
if c in text:
text = text.replace(c, "\\" + c)
``````

In the case of the shorter input:

With the more extensive input:

So, for readability and speed, I’m going to use ba.

Prompted by haccks in the comments, one difference between ab and ba is the if c in text: check. Let’s test them against two more variants:

``````def ab_with_check(text):
for ch in ['\\','`','*','_','{','}','[',']','(',')','>','#','+','-','.','!','\$','\'']:
if ch in text:
text = text.replace(ch,"\\"+ch)

def ba_without_check(text):
chars = "\\`*_{}[]()>#+-.!\$"
for c in chars:
text = text.replace(c, "\\" + c)
``````

Times in seconds per loop on Python 2.7.14 and 3.6.3, on a different machine than the previous batch, so they can’t be directly compared.

``````╭────────────╥──────┬───────────────┬──────┬──────────────────╮
│ Py, input  ║  ab  │ ab_with_check │  ba  │ ba_without_check │
╞════════════╬══════╪═══════════════╪══════╪══════════════════╡
│ Py2, short ║ 8.81 │    4.22       │ 3.45 │    8.01          │
│ Py3, short ║ 5.54 │    1.34       │ 1.46 │    5.34          │
├────────────╫──────┼───────────────┼──────┼──────────────────┤
│ Py2, long  ║ 9.3  │    7.15       │ 6.85 │    8.55          │
│ Py3, long  ║ 7.43 │    4.38       │ 4.41 │    7.02          │
└────────────╨──────┴───────────────┴──────┴──────────────────┘
``````

We can draw the following conclusion:

## Solution #2

``````>>> string="abc&def#ghi"
>>> for ch in ['&','#']:
...   if ch in string:
...      string=string.replace(ch,"\\"+ch)
...
>>> print string
abc\&def\#ghi
``````

## Solution #3

Using str.translate and str.maketrans, here’s a python3 method:

``````s = "abc&def#ghi"
print(s.translate(str.maketrans({'&': '\&', '#': '\#'})))
``````

abc&def#ghi is the printed string.

## Solution #4

Simply connect the replace functions in a chain like this.

``````strs = "abc&def#ghi"
print strs.replace('&', '\&').replace('#', '\#')
# abc\&def\#ghi
``````

If the replacements will be more numerous, you can use this generic method.

``````strs, replacements = "abc&def#ghi", {"&": "\&", "#": "\#"}
print "".join([replacements.get(c, c) for c in strs])
# abc\&def\#ghi
``````

## Solution #5

Will you always use a backslash as a prefix? If so, give it a shot.

``````import re
rx = re.compile('([&#])')
#                  ^^ fill in the characters here.
strs = rx.sub('\\\\\\1', strs)
``````

cient way, but I believe it is the most straightforward.