469,283 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

bizarre behavior using .lstrip

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
s = 'chg bonn_fee'
print s chg bonn_fee print s.lstrip('chg ') bonn_fee
s = 'chg conn_fee'
print s chg conn_fee print s.lstrip('chg ') onn_fee
Does this make any sense at all? where did the lead c in conn_fee go?

Note that if I just strip 'chg' and not the one whitespace character
next to it I get the expected result:
print s.lstrip('chg') conn

but extending the right quote by one space not only strips the
whitespace but the c:
print s.lstrip('chg ')

onn
Not really sure what's causing this, but it's making me change the
text parser I'm 600 lines into.
Jul 18 '05 #1
8 2615
Pete Jereb wrote:

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
s = 'chg bonn_fee'
print s chg bonn_fee print s.lstrip('chg ') bonn_fee
s = 'chg conn_fee'
print s chg conn_fee print s.lstrip('chg ')

onn_fee

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.

-Peter
Jul 18 '05 #2
As an alternative you could use re.sub()
import re
s = 'chg cbonn_fee'
print re.sub('chg ', '', s)
cbonn_fee
On Friday 19 September 2003 8:11 pm, Peter Hansen wrote: Pete Jereb wrote:
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
>> s = 'chg bonn_fee'
>> print s


chg bonn_fee
>> print s.lstrip('chg ')


bonn_fee
>> s = 'chg conn_fee'
>> print s


chg conn_fee
>> print s.lstrip('chg ')


onn_fee

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.

-Peter

Jul 18 '05 #3
whoops! Forgot the '^' in the regex...
import re
s = 'chg cbonn_fee'
print re.sub('^chg ', '', s)


On Friday 19 September 2003 10:43 pm, Jeremy Dillworth wrote:
As an alternative you could use re.sub()
import re
s = 'chg cbonn_fee'
print re.sub('chg ', '', s)


cbonn_fee

On Friday 19 September 2003 8:11 pm, Peter Hansen wrote:
Pete Jereb wrote:
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32

>>> s = 'chg bonn_fee'
>>> print s

chg bonn_fee

>>> print s.lstrip('chg ')

bonn_fee

>>> s = 'chg conn_fee'
>>> print s

chg conn_fee

>>> print s.lstrip('chg ')

onn_fee

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.

-Peter

Jul 18 '05 #4
On 19 Sep 2003 17:07:47 -0700, in article
<68**************************@posting.google.com >, Pete Jereb wrote:
s = 'chg conn_fee'
print s chg conn_fee print s.lstrip('chg ') onn_fee

Does this make any sense at all? where did the lead c in conn_fee go?
It was removed, as you requested. lstrip('chg ') means remove every
occurence of space, 'g', 'h', and 'c' from the front (left) of the string.
print s.lstrip('chg') conn


lstrip('chg') stops stripping when it finds a character not in 'chg'. The
space is the first character not 'chg' so that's where lstrip stops
stripping.
print s.lstrip('chg ')

onn


lstrip('chg ') stops stripping when it finds a character not in 'chg '.
The 'o' is the first character not in 'chg' so that's where lstrip stops
stripping.
Not really sure what's causing this [...]
As they said where I used to work, WAD: it Works As Designed.
[...] but it's making me change the
text parser I'm 600 lines into.


It's not clear what you're trying to do. Do you want to remove all the
leading space, 'g', 'h' and 'c' characters from the string, or do you want
to remove the leading (exact) string "chg " as a whole?

One way to remove the leading 'g', 'h', and 'c' characters from the front
of the string first, and then remove spaces from the resulting string

print s.lstrip('chg').lstrip()
Jul 18 '05 #5

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@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
Jul 18 '05 #6
Thanks to all for the clarification. I was under the impression that
lstrip would remove a string I specified, not any character in the
string until a character is no longer found. I like the

s.lstrip('chg').lstrip

solution.

This is my first Python program, and between the messages on this
group and "Python in a Nutshell" I've been able to solve all the
problems I've encountered thus far. Hopefully I'll be able to
contribute to this group in a little bit! Pete.
Jul 18 '05 #7
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

Jul 18 '05 #8
Terry Reedy wrote:

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@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.


I think that last phrase might be responsible for the OP's confusion.
Even now my brain interprets it as meaning the *string* formed by those
characters will be removed from the beginning of the string, not that
each occurrence of any character in that arg string will be removed
from the target.

-Peter
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Brian | last post: by
4 posts views Thread by joram gemma | last post: by
14 posts views Thread by Michael Carr | last post: by
1 post views Thread by Michael Carr | last post: by
3 posts views Thread by Peter | last post: by
2 posts views Thread by Kelie | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.