By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

strip question

P: n/a
hi
can someone explain strip() for these :
Expand|Select|Wrap|Line Numbers
  1.         
  2.                         
  3.                         
  4.                 >>x='www.example.com'
  5. x.strip('cmowz.')
  6.  
  7. 'example'
  8.  
when i did this:
Expand|Select|Wrap|Line Numbers
  1.         
  2.                         
  3.                         
  4.                 >>x = 'abcd,words.words'
  5. x.strip(',.')
  6.  
  7. 'abcd,words.words'
  8.  
it does not strip off "," and "." .Why is this so?
thanks

Jan 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ei***********@yahoo.com wrote:
hi
can someone explain strip() for these :
Expand|Select|Wrap|Line Numbers
  1.         
  2.                         
  3.                 >>>>x='www.example.com'
  4. x.strip('cmowz.')
  •  
  • 'example'
  •  

  • when i did this:
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                 >>>>x = 'abcd,words.words'
    4. x.strip(',.')
    5.  
    6. 'abcd,words.words'
    7.  

    it does not strip off "," and "." .Why is this so?
    thanks
    strip strips from the ends.
    pyx = '...,.,abcd,words.words,,,,.,.,.,.,'
    pyx.strip(',.')
    'abcd,words.words'
    James
    Jan 27 '07 #2

    P: n/a
    On Fri, 26 Jan 2007 21:33:47 -0800, eight02645999 wrote:
    hi
    can someone explain strip() for these :
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                 >>>x='www.example.com'
    4. x.strip('cmowz.')
  •  
  • 'example'
  •  

  • when i did this:
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                 >>>x = 'abcd,words.words'
    4. x.strip(',.')
    5.  
    6. 'abcd,words.words'
    7.  

    it does not strip off "," and "." .Why is this so?
    thanks
    Fascinating...

    It gets weirder:
    >>x.strip('s')
    'abcd,words.word'

    Why strip only the final s, not the earlier one?
    >>x.strip('w')
    'abcd,words.words'
    >>x.strip('o')
    'abcd,words.words'
    >>x.strip('r')
    'abcd,words.words'

    Strips nothing.
    >>x.strip('ba')
    'cd,words.words'

    Strips correctly.
    >>x.strip('bwa')
    'cd,words.words'

    Strips the a and b but not the w.
    >>x.strip('bwas')
    'cd,words.word'

    ....and only one of the S's.
    >>y = "bwas"
    y.strip('bwas')
    ''
    >>y = "bwasxyz"
    y.strip('bwas')
    'xyz'

    And yet these work.
    You know, I'm starting to think there may be a bug in the strip method...
    either that or the documentation should say:

    strip(...)
    S.strip([chars]) -string or unicode

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove none, some or all characters in
    chars instead. If chars is unicode, S will be converted to unicode
    before stripping
    *wink*
    --
    Steven.

    Jan 27 '07 #3

    P: n/a
    Steven D'Aprano wrote:
    On Fri, 26 Jan 2007 21:33:47 -0800, eight02645999 wrote:

    >>hi
    can someone explain strip() for these :
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                 >>>>>x='www.example.com'
    3. >x.strip('cmowz.')
  •  
  • 'example'

  • when i did this:
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                 >>>>>x = 'abcd,words.words'
    3. >x.strip(',.')
    4.  
    5. 'abcd,words.words'

    it does not strip off "," and "." .Why is this so?
    thanks
    Fascinating...

    It gets weirder:

    >>>>x.strip('s')

    'abcd,words.word'

    Why strip only the final s, not the earlier one?

    >>>>x.strip('w')

    'abcd,words.words'
    >>>>x.strip('o')

    'abcd,words.words'
    >>>>x.strip('r')

    'abcd,words.words'

    Strips nothing.

    >>>>x.strip('ba')

    'cd,words.words'

    Strips correctly.

    >>>>x.strip('bwa')

    'cd,words.words'

    Strips the a and b but not the w.

    >>>>x.strip('bwas')

    'cd,words.word'

    ...and only one of the S's.

    >>>>y = "bwas"
    y.strip('bwas')

    ''
    >>>>y = "bwasxyz"
    y.strip('bwas')

    'xyz'

    And yet these work.
    You know, I'm starting to think there may be a bug in the strip method...
    either that or the documentation should say:

    strip(...)
    S.strip([chars]) -string or unicode

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove none, some or all characters in
    chars instead. If chars is unicode, S will be converted to unicode
    before stripping
    *wink*

    At the risk of appearing that I didn't notice your *wink*, the operative
    words in the docs would be "leading" and "trailing". But somehow I think
    you knew that.

    James
    Jan 27 '07 #4

    P: n/a


    On Jan 27, 4:33 pm, eight02645...@yahoo.com wrote:
    hi
    can someone explain strip() for these :
    Expand|Select|Wrap|Line Numbers
    1. >>x='www.example.com'
    2.         
    3.                         
    4.                 >x.strip('cmowz.')'example'
  •  
  •  
  • According to the documentation (with emphasis added):

    """Return a copy of the string with the *leading* and *trailing*
    characters removed."""

    Looking at the leading edge: "w" is in the set to strip, so it is
    removed. Same applies to another 2 "w"s and a dot. "e" is not in the
    strip set so it stops.
    At the trailing edge: "m", "o", "c" and dot are removed, then it stops
    because there's another "e".
    when i did this:
    Expand|Select|Wrap|Line Numbers
    1. >>x = 'abcd,words.words'
    2.         
    3.                         
    4.                 >x.strip(',.')'abcd,words.words'
  •  
  •  

  • it does not strip off "," and "." .Why is this so? The comma and dot in your example are neither leading nor trailing.
    The leading "a" is not in the set to strip, so nothing happens at the
    front door.
    The trailing "s" is not in the set to strip, so there's no back door
    action either.

    HTH,
    John

    Jan 27 '07 #5

    P: n/a
    ei***********@yahoo.com a écrit :
    hi
    can someone explain strip() for these :
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                 >>>>x='www.example.com'
    4. x.strip('cmowz.')
  •  
  • 'example'
  •  

  • when i did this:
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                 >>>>x = 'abcd,words.words'
    4. x.strip(',.')
    5.  
    6. 'abcd,words.words'
    7.  

    it does not strip off "," and "." .Why is this so? Probably because the Fine Manual(tm) says that str.strip() removes
    heading and trailing chars ?

    """
    bruno@bibi ~ $ python
    Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
    [GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
    linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>help(''.strip)
    Help on built-in function strip:

    strip(...)
    S.strip([chars]) -string or unicode

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    If chars is unicode, S will be converted to unicode before stripping
    """
    You may want to try str.replace() instead:

    """
    >>help(''.replace)
    Help on built-in function replace:

    replace(...)
    S.replace (old, new[, count]) -string

    Return a copy of string S with all occurrences of substring
    old replaced by new. If the optional argument count is
    given, only the first count occurrences are replaced.
    >>'abcd,words.words'.replace(',', '').replace('.', '')
    'abcdwordswords'
    """
    thanks
    HTH

    Jan 29 '07 #6

    P: n/a
    On 26 Jan 2007 21:33:47 -0800, ei***********@yahoo.com wrote:
    >hi
    can someone explain strip() for these :
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                 >>>x='www.example.com'
    4. x.strip('cmowz.')
  •  
  • 'example'

  • when i did this:
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                 >>>x = 'abcd,words.words'
    4. x.strip(',.')
    5.  
    6. 'abcd,words.words'

    it does not strip off "," and "." .Why is this so?
    thanks If you only have a couple of characters to deal with then use
    replace(). Otherwise use string.translate() :
    >>import string
    x = 'abcd,words.words'
    transform = string.maketrans(',.','..')
    x = string.translate(x, transform)
    x = x.replace('.','')
    x
    'abcdwordswords''
    Dan
    Jan 30 '07 #7

    This discussion thread is closed

    Replies have been disabled for this discussion.