472,348 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

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 2765
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
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 >>>...
4
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...
14
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...
1
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...
0
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...
1
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 &...
3
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,...
35
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...
2
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...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.