473,545 Members | 1,717 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 2848
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(p refix, s):
"""remove_prefi x(prefix, s) -> str
If s starts with prefix, return the part of s remaining after
prefix. Otherwise, return the original string"""
if s.startswith(pr efix):
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
2255
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' >>> string.lstrip("test/one/two/a.b.c", "test/one") 'wo/a.b.c' >>> string.lstrip("test/one/two/a.b.c", "test/one/tw") 'a.b.c'
4
3318
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 >>> t = 'D:\\music\\D\\' >>> print t D:\music\D\
14
2043
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 described below occurs for 0.1% of users, and so far I've only seen it on IE 6.0. My ASP.NET website is set up like this: 1. Logon page - Collect...
1
1342
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 works fine. However, if you open both of them simultaneously in two frames of a frameset, wierdness ensues. To demonstrate this, open the following...
0
1956
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; the graphic is supplied as a "StdPicture" (AKA, IPictureDisp). The only way I have found to convert this to a .NET Image is: ...
1
2684
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 & vbCrLf & "Credit Card Number: " & objBOWallet.Encrypt(Payment.) If Not IsNull(Payment.) Then MT = MT & vbCrLf & "Expiration Month: " & Payment. End...
3
1876
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 have a DataView which filters a DataSet. Bound to this dataview is a ListBox, via its DataSource property. The DisplayMember is the name property of...
35
2174
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 code. This example is trivial and has design issues, but it demonstrates a problem I've seen in production systems: def main(argv = ): 'print...
2
2107
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: s2 = s.upper().lstrip(chars.upper()) return s else:
0
7453
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7410
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7742
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5318
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4940
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1865
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1007
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.