Thread: Stripping

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    44
    Rep Power
    11

    Stripping


    I'm a little bit confused with strip function in module STRING sometime it works sometime not.For example

    Code:
    >> a = 'myname/is'
    >> print string.strip(a,'/')
    
    >> myname/is
    so what's wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    How is that not working?
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Code:
    >>> import string
    >>> help(string.strip)
    Help on function strip in module string:
    
    strip(s, chars=None)
        strip(s [,chars]) -> string
        
        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.
    >>> string.strip('myname/is', '/')
    'myname/is'
    >>> string.strip('///myname/is///', '/')
    'myname/is'
    >>>
    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    BTW, the functions in the string module are the old way of doing it. Unless your code needs to be backwards compatible with Python 1.5.2 and earlier, then the preferred way is to use the methods on the string object itself:

    Code:
    >>> '///myname/is///'.strip('/')
    'myname/is'
    >>>
    Dave - The Developers' Coach
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you want to replace all occurances of particular strings/chars then you should look at the replace() method - very handy, i seem to use it when ever i do any string manipulation.

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    44
    Rep Power
    11
    Originally Posted by netytan
    If you want to replace all occurances of particular strings/chars then you should look at the replace() method - very handy, i seem to use it when ever i do any string manipulation.

    Have fun,

    Mark.
    Yeah that one is COOL .. screw strip function.

IMN logo majestic logo threadwatch logo seochat tools logo