I use Python 2.6 and I want to replace each instance of certain leading characters (., _ and $ in my case) in a string with another character or string. Since in my case the replacement string is the same, I came up with this:
def replaceLeadingCharacters(string, old, new = ''):
t = string.lstrip(old)
return new * (len(string) - len(t)) + t
which seems to work fine:
>>> replaceLeadingCharacters('._.!$XXX$._', '._$', 'Y')
'YYY!$XXX$._'
-
Is there a better (simpler or more efficient) way to achieve the same effect in Python ?
-
Is there a way to achieve this effect with a string instead of characters? Something like str.replace() that stops once something different than the string-to-be-replaced comes up in the input string? Right now I’ve come up with this:
def replaceLeadingString(string, old, new = ''): n = 0 o = 0 s = len(old) while string.startswith(old, o): n += 1 o += s return new * n + string[o:]I am hoping that there is a way to do this without an explicit loop
EDIT:
There are quite a few answers using the re module. I have a couple of questions/issues with it:
-
Isn’t it significantly slower than the
strmethods when used as a replacement for them? -
Is there an easy way to properly quote/escape strings that will be used in a regular expression? For example if I wanted to use
reforreplaceLeadingCharacters, how would I ensure that the contents of theoldvariable will not mess things up in^[old]+? I’d rather have a “black-box” function that does not require its users to pay attention to the list of characters that they provide.
Your
replaceLeadingCharacters()seems fine as is.Here’s
replaceLeadingString()implementation that usesremodule (without thewhileloop):Don’t guess. Measure it for expected input.
re.escape()