Let's say there is a string of any length, and it only contains the letters A through D:
s1 = 'ACDCADBCDBABDCBDAACDCADCDAB'
What is the most efficient/fastest way to replace every 'B' with an 'C' and every 'C' with a 'B'.
Heres what I am doing now:
replacedString = '' for i in s1: if i == 'B': replacedString += 'C' elif i == 'C': replacedString += 'B' else: replacedString += i
This works but it is obviously not very elegant. The probelm is that I am dealing with strings that can be ones of milliions of characters long, so I need a better solution.
I can't think of a way to do this with the .replace() method. This suggests that maybe a regular expression is the way to go. Is that applicable here as well? If so what is a suitable regular expression? Is there an even faster way?
Apart from the
str.translate method, you could simply build a translation dict and run it yourself.
s1 = 'ACDCADBCDBABDCBDAACDCADCDAB' def str_translate_method(s1): try: translationdict = str.maketrans("BC","CB") except AttributeError: # python2 import string translationdict = string.maketrans("BC","CB") result = s1.translate(translationdict) return result def dict_method(s1): from, to = "BC", "CB" translationdict = dict(zip(from, to)) result = ' '.join([translationdict.get(c, c) for c in s1]) return result