473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

strip question

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
6 2326
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
    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
    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


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

    Similar topics

    16
    by: jcf | last post by:
    I need to do the following: Our application builds information from a string that is stored in a buffer that can be any of the following formats: ABCD0102, ABCDEF0102, AB*CD*01*02, AB*CDEF*01*02....
    1
    by: Smoke | last post by:
    Im testing the new June CTP release of VS2005 and each time i try to use the Status Strip or Tool Strip control at design time i get and object reference not set to an instance of an object error,...
    5
    by: dan.j.weber | last post by:
    I'm using Python 2.3.5 and when I type the following in the interactive prompt I see that strip() is not working as advertised: >>>s = 'p p:p' >>>s.strip(' :') 'p p:p' Is this just me or...
    6
    by: rtilley | last post by:
    s = ' qazwsx ' # How are these different? print s.strip() print str.strip(s) Do string objects all have the attribute strip()? If so, why is str.strip() needed? Really, I'm just curious......
    4
    by: Adam Honek | last post by:
    Hi all, The tool strip control is set to flow layout, no autosize and to anchor top, left. When the form is maximized all the icons and text fit in one line across the screen. The menu...
    9
    by: amattie | last post by:
    Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has...
    3
    by: Colin J. Williams | last post by:
    The Library Reference has strip( ) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed....
    6
    by: Christoph Zwerschke | last post by:
    In Python programs, you will quite frequently find code like the following for removing a certain prefix from a string: if url.startswith('http://'): url = url Similarly for stripping...
    4
    by: Poppy | last post by:
    I'm using versions 2.5.2 and 2.5.1 of python and have encountered a potential bug. Not sure if I'm misunderstanding the usage of the strip function but here's my example. var = "detail.xml"...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    0
    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,...
    0
    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
    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
    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
    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...
    0
    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...

    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.