473,508 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2841
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2250
by: Brian | last post by:
Hello - has anyone else had trouble with string.lstrip stripping too much? Here's what I get (Python 2.3.4): >>> import string >>> string.lstrip("test/one/two/a.b.c", "test/one/") 'wo/a.b.c'...
4
3312
by: joram gemma | last post by:
Hello, on windows python 2.4.1 I have the following problem >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona' >>> print s D:\music\D\Daniel Lanois\For the beauty of Wynona >>>...
14
2040
by: Michael Carr | last post by:
I have an intermittent problem that occurs on a very small number of browsers that access my website. The strange thing is this: my site works perfectly for 99.9% of browsers. The behavior...
1
1339
by: Michael Carr | last post by:
I have created a website that responds to the following two domain names: logintest.carr-engineering.com logintest.onualumni.org If you open either of these websites on their own, everything...
0
1949
by: Mark | last post by:
We're authoring a VS 2005 app that includes several EXE's and DLL's and also uses a COM component (a customer requirement). The COM component provides a graphic image to be used by the .NET app;...
1
2680
by: zoehart | last post by:
I'm working with VBScript to build a text email message. I'm seeing a variety of bizarre formatting issues. The following lines of code MT = MT & vbCrLf & "Card Type: " & CardType MT = MT &...
3
1872
by: Peter | last post by:
Hi! I am having some very strange behavior with my databound controls. It's taken a long time to isolate exactly what is provoking the problem, but I'm still leagues away from solving it. I...
35
2162
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
2
2104
by: Kelie | last post by:
Hello, Is there such a function included in the standard Python distribution? This is what I came up with. How to improve it? Thanks. def lstrip2(s, chars, ingoreCase = True): if ingoreCase:...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.