Re: Problem with the strip string method
Thanks to all respondents, Steve Holden is right, I expected more than I should have. Others have explained why all your examples work as they should. From your exmaples, it seems like you would like strip to remove the leading and trailing characters from EVERY LINE in your string. This can be done by the simple construct my_string = ' foo\n bar ' '\n'.join(line.strip() for line in my_string.split('\n')) 'foo\nbar' If you need this construct at several places, define a function def line_strip(string,sep='\n') : return sep.join(line.strip() for line in string.split(sep)) cheers, - harold - -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with the strip string method
Colin J. Williams wrote: Return a copy of the string with the leading and trailing characters removed. Only the last two examples below behave as expected. They all looks OK to me. [Dbg] 'ab$%\n\rcd'.strip('%') 'ab$%\n\rcd' No % at the beginning or end of string. Nothing changed. [Dbg] 'ab$%cd'.strip('$') 'ab$%\n\rcd' No $ at the beginning or end of string. Nothing changed. I believe that you didn't copy this from the standard input due to the presence of \r\n on the answer... [Dbg] 'ab$%cd'.strip('$') 'ab$%cd' No $ at the beginning or end of string. Nothing changed. [Dbg] ' ab$%cd '.strip('$') ' ab$%cd ' No $ at the beginning or end of string. Nothing changed. [Dbg] ' ab$%cd '.strip('%') ' ab$%cd ' No % at the beginning or end of string. Nothing changed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with the strip string method
Colin J. Williams schrieb The Library Reference has strip( [chars]) Return a copy of the string with the leading and trailing characters removed. It's leading and trailing, not leading, trailing or embedded. xxxaaaxxx.strip(x) 'aaa' xxxaaaxxxaaaxxx.strip(x) 'aaaxxxaaa' HTH Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with the strip string method
Colin J. Williams wrote: The Library Reference has 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' Only the last two examples below behave as expected. Adjust your expectations. The software is correct. Is it intended that the full range of characters be handled? Colin W. [Dbg] 'ab$%\n\rcd'.strip('%') 'ab$%\n\rcd' [Dbg] 'ab$%cd'.strip('$') 'ab$%\n\rcd' [Dbg] 'ab$%cd'.strip('$') 'ab$%cd' [Dbg] ' ab$%cd '.strip('$') ' ab$%cd ' [Dbg] ' ab$%cd '.strip('%') ' ab$%cd ' [Dbg] ' spacious '.strip() 'spacious' [Dbg] 'www.example.com'.strip('cmowz.') 'example' I suspect what you need is the .replace() method. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with the strip string method
On Mar 2, 11:45 am, Steve Holden [EMAIL PROTECTED] wrote: I suspect what you need is the .replace() method. The information's there-- the word 'contiguous' might clear it up a bit. 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: Return the string's substring from the first character not a member of 'chars' to the last such. Remove contiguous leading and trailing members of 'chars'. If omitted or None, 'chars' defaults over to the set of whitespace set( \n\r\t ). (XXX TODO: ask Steve Reg Ex Guru this). -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with the strip string method
[EMAIL PROTECTED] wrote: On Mar 2, 11:45 am, Steve Holden [EMAIL PROTECTED] wrote: I suspect what you need is the .replace() method. The information's there-- the word 'contiguous' might clear it up a bit. 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: Return the string's substring from the first character not a member of 'chars' to the last such. Remove contiguous leading and trailing members of 'chars'. If omitted or None, 'chars' defaults over to the set of whitespace set( \n\r\t ). (XXX TODO: ask Steve Reg Ex Guru this). Thanks to all respondents, Steve Holden is right, I expected more than I should have. Colin W. -- http://mail.python.org/mailman/listinfo/python-list