Odd strip behavior
#!/usr/bin/python def main(): 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 ? Sent from my iPhone -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd strip behavior
On Mar 22, 2012 7:49 PM, "Rodrick Brown" wrote: > > #!/usr/bin/python > > def main(): > >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 ? Try help(ste.strip) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
RE: Odd strip behavior
> 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@python.org > [mailto:python-list-bounces+ramit.prasad=jpmorgan@python.org] On Behalf > Of Rodrick Brown > Sent: Thursday, March 22, 2012 2:49 PM > To: python-list@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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd strip behavior
On 3/22/2012 20:48, Rodrick Brown wrote: #!/usr/bin/python def main(): 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 ? Because it's not a leading or trailing character. Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd strip behavior
strip() removes leading and trailing characters, which is why the 't' in the middle of the string was not removed. To remove the 't' in the middle, str1.replace('t','') is one option. On 3/22/12 3:48 PM, Rodrick Brown wrote: #!/usr/bin/python def main(): 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 ? Sent from my iPhone -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd strip behavior
On Mar 22, 2012, at 3:53 PM, Arnaud Delobelle wrote: > > On Mar 22, 2012 7:49 PM, "Rodrick Brown" wrote: > > > > #!/usr/bin/python > > > > def main(): > > > >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 ? > > Try help(ste.strip) > It clearly states "if chars is given and not None, remove characters in chars instead. Does it mean remove only the first occurrence of char? That's the behavior I'm seeing. > -- > Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd strip behavior
On 22 March 2012 20:04, Rodrick Brown wrote: > > On Mar 22, 2012, at 3:53 PM, Arnaud Delobelle wrote: > Try help(ste.strip) > > It clearly states "if chars is given and not None, remove characters in > chars instead. > > Does it mean remove only the first occurrence of char? That's the behavior > I'm seeing. The key words in the docstring are "leading and trailing" -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd strip behavior
On Thu, Mar 22, 2012 at 1:48 PM, Rodrick Brown wrote: > Why wasnt the t removed ? Because str.strip() only removes leading or trailing characters. If you want to remove all the t's, use str.replace: 'this is a test'.replace('t', '') Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list