bizarre behavior using .lstrip
Terry Reedy
tjreedy at udel.edu
Sat Sep 20 00:41:15 EDT 2003
"Peter Hansen" <peter at engcorp.com> wrote in message
news:3F6B9B44.9EAD3CF9 at engcorp.com...
> > Does this make any sense at all? where did the lead c in conn_fee
go?
>
> Based on the behaviour you describe, I would assume lstrip()
> removes, starting at the beginning of the string, all characters
> which are *anywhere* in the argument string you give it, until
> it encounters a character not in that string, at which point it
> stops.
Good call: from Lib Ref 2.2.6.1 String Methods
lstrip( [chars])
Return a copy of the string with leading characters removed. If chars
is omitted or None, whitespace characters are removed. If given and
not None, chars must be a string; the characters in the string will be
stripped from the beginning of the string this method is called on.
If one wants to remove a leading substring, s[k:] does nicely.
Determining a common prefix to remove is also easy. The following
does what OP expected:
def rempre(s, pre):
'assume len(pre) <= len(s)'
stop = len(pre)
i = 0
while i < stop and s[i] == pre[i]:
i += 1
return s[i:]
>>> rempre(s, 'chg')
' conn'
>>> rempre(s, 'chg ')
'conn'
Terry J. Reedy
More information about the Python-list
mailing list