Odd strip behavior

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Mar 22 15:54:37 EDT 2012


>     str1='this is a test'
>     str2='t'
> 
>     print "".join([ c for c in str1 if c not in str2 ])
>     print(str1.strip(str2))
> 
> if __name__ == '__main__':
>     main()
> 
> ./remove_str.py
> his is a es
> his is a tes
> 
> Why wasnt the t removed ?

This is not odd behavior, you just do not understand what
strip does. :)

The behavior is listed on the API. I highly recommend you 
take time to become familiar with it. 


http://docs.python.org/library/stdtypes.html#string-methods
str.strip([chars])
    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. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
    >>>
    >>> '   spacious   '.strip()
    'spacious'
    >>> 'www.example.com'.strip('cmowz.')
    'example'


    Changed in version 2.2.2: Support for the chars argument.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


> -----Original Message-----
> From: python-list-bounces+ramit.prasad=jpmorgan.com at python.org
> [mailto:python-list-bounces+ramit.prasad=jpmorgan.com at python.org] On Behalf
> Of Rodrick Brown
> Sent: Thursday, March 22, 2012 2:49 PM
> To: python-list at python.org
> Subject: Odd strip behavior
> 
> #!/usr/bin/python
> 
> def main():
> 
> Sent from my iPhone
> --
> http://mail.python.org/mailman/listinfo/python-list
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list