bizarre behavior using .lstrip
Jeff Epler
jepler at unpythonic.net
Sat Sep 20 10:43:48 EDT 2003
On Fri, Sep 19, 2003 at 10:43:27PM -0400, Jeremy Dillworth wrote:
> As an alternative you could use re.sub()
>
> >>> import re
> >>> s = 'chg cbonn_fee'
> >>> print re.sub('chg ', '', s)
> cbonn_fee
I'd recommend avoiding regexes if this is all you're doing. Instead,
you can write a function to do it:
def remove_prefix(prefix, s):
"""remove_prefix(prefix, s) -> str
If s starts with prefix, return the part of s remaining after
prefix. Otherwise, return the original string"""
if s.startswith(prefix):
return s[len(prefix):]
return s
>>> for s in ("chg conn_fee", "something else", "chg x", "chg", "chg "):
... remove_prefix("chg ", s)
...
'conn_fee'
'something else'
'x'
'chg'
''
Jeff
More information about the Python-list
mailing list