Could someone explain this to a newbie

Discussion in 'Python' started by Sean Berry, Apr 27, 2004.

  1. Sean Berry

    Sean Berry Guest

    >>> text = 'zone "southernpine.com" {'
    >>> text.lstrip("zone \"")

    'southernpine.com" {'

    This is expected.


    >>> text = 'zone "newsouthpine.com" {'
    >>> text.lstrip("zone \"")

    'wsouthpine.com" {'

    This is not.

    What happened to the ne...

    I suppose that is the secret, because I have
    tried with other words beginning with ne and
    they do not work either.

    --
     
    Sean Berry, Apr 27, 2004
    #1
    1. Advertising

  2. Sean Berry wrote:
    > >>> text = 'zone "southernpine.com" {'
    > >>> text.lstrip("zone \"")

    > 'southernpine.com" {'
    >
    > This is expected.
    >
    >
    > >>> text = 'zone "newsouthpine.com" {'
    > >>> text.lstrip("zone \"")

    > 'wsouthpine.com" {'
    >
    > This is not.
    >
    > What happened to the ne...
    >
    > I suppose that is the secret, because I have
    > tried with other words beginning with ne and
    > they do not work either.


    Sean, try this:

    >>> text = 'abcabcaabbccaaabbbcccxyz'
    >>> text.lstrip('abc')

    'xyz'
    >>>


    Do you see what lstrip() really does now?

    Your confusion is understandable. The documentation for lstrip() is very
    poor--it does not make it at all clear what the function does. The argument
    to lstrip() isn't a string that the function strips off, it is a *set of
    characters* (expressed as a string) to strip off.

    -Mike
     
    Michael Geary, Apr 27, 2004
    #2
    1. Advertising

  3. Am Dienstag, 27. April 2004 19:45 schrieb Sean Berry:
    > >>> text = 'zone "newsouthpine.com" {'
    > >>> text.lstrip("zone \"")

    >
    > 'wsouthpine.com" {'
    >


    From the Python Manual:

    ""
    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. Changed in version
    2.2.2: Support for the chars argument.
    ""

    Thus, it does exactly as it sais. The first few characters after the "zone \""
    in the string are contained in the chars array which you pass in (namely
    "ne"), and thus they are also stripped from the string.

    What you might want to do is something like the following:

    x = x.strip()
    if x.startswith("zone \""): x = x[6:]

    HTH!

    Heiko.
     
    Heiko Wundram, Apr 27, 2004
    #3
  4. Heiko Wundram <> writes:

    > in the string are contained in the chars array which you pass in (namely
    > "ne"), and thus they are also stripped from the string.


    Maybe the docs ought to state that the method stops once it hits a
    char not in the *set* the passed string represents.
     
    Tor Iver Wilhelmsen, Apr 28, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gactimus
    Replies:
    22
    Views:
    742
    jeffc
    Dec 15, 2004
  2. Replies:
    5
    Views:
    305
    Bo Persson
    Jan 10, 2006
  3. Alexander Mahone

    Could someone please explain me this error?

    Alexander Mahone, May 2, 2008, in forum: C Programming
    Replies:
    5
    Views:
    281
    Igmar Palsenberg
    May 5, 2008
  4. wkevin
    Replies:
    4
    Views:
    606
    Edward A. Falk
    Nov 7, 2011
  5. lg
    Replies:
    5
    Views:
    128
Loading...

Share This Page