[Tutor] creating the equivalent of string.strip()

Andrew James aijames at exemail.com.au
Mon Oct 1 02:23:16 CEST 2007


I've gone ahead and created a script that does this, however it also 
strips punctuation. I was originally just comparing each character to a 
string containing a single space ' ' but even using s[-1].isspace() I 
lose punctuation marks. Any idea why that's happening?

(Not the OP, I just thought this would be interesting to do)

Bob Nienhuis wrote:
> BTW, GMail brings up some interesting sponsored links when the subject 
> line
> has string.strip in it ( :-0)
>
> On 9/30/07, *Alan Gauld* < alan.gauld at btinternet.com 
> <mailto:alan.gauld at btinternet.com>> wrote:
>
>
>     "wesley chun" < wescpy at gmail.com <mailto:wescpy at gmail.com>> wrote
>     in message
>     news:78b3a9580709300102x12cbe392uebb988ed965c7202 at mail.gmail.com...
>     >> > I'm not sure how to proceed.  My biggest stumbling
>     >> > block is how to detect the leading and trailing
>     >> > whitespace.
>     >>
>     >> Use indexing.
>     >> Try using a while loop.
>     >> Or maybe two?
>     >>
>     >> And strings have an isspace method...
>     >
>     > unfortunately, the isspace() string method only returns True if all
>     > chars in the string are whitespace chars and False otherwise, so
>     > that's a bummer.
>
>     But the string can be one character long:
>
>     s = 'fred\n'
>     print s[-1].isspace()      # --> True
>
>     :-)
>
>     Alan G
>
>
>     _______________________________________________
>     Tutor maillist  -  Tutor at python.org <mailto:Tutor at python.org>
>     http://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.488 / Virus Database: 269.13.35 - Release Date: 29/09/2007 12:00 AM
>   


More information about the Tutor mailing list