Optional argument for string.strip() (Was: [Python-Dev] Small fea ture request)

Simon Brunning SBrunning at trisystems.co.uk
Tue Jul 31 04:23:08 EDT 2001


>From:	aahz at rahul.net [SMTP:aahz at rahul.net]
>Simon Brunning wrote:
>> 
>> This comes up on c.l.py occasionally, though - "What is Python's
equivalent
>> of Perl's 'chomp' function?" The .rstrip() method isn't it, 'cos it
strips
>> all whitespace, but often people want only the line separators removed.
if
>> rstrip took an argument, you could do:

>Ah.  Yes, that one.  I think that ought to be better fixed with an
>actual chomp() function, but the argument against it has always been
>that there's no good way to do it in the face of files from other
>platforms....  You wanna talk about that one, we might be able to have a
>productive discussion on c.l.py.

Will do...

To recap:

The .split method on strings splits at whitespace by default, but takes an
optional argument allowing splitting by other strings. The .strip method
(and its siblings) always strip whitespace - on more than one occasion I
would have found it useful if these methods also took an optional argument
allowing other strings to be stripped. For example, to strip, say, asterisks
from a string you could do:

>>>fred = '**word**word**'
>>>fred.strip('*')
word**word

As I mentioned, this would give us an equivalent to Perl's 'chomp'. Does
this sound sensible/useful?

BTW, my spell checker suggests that 'Perl' should be spelt 'Peril'. Smarter
than I thought.

Cheers,
Simon Brunning.





-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.




More information about the Python-list mailing list