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

2007-10-04 Thread Alan Gauld
"Christopher Spears" <[EMAIL PROTECTED]> wrote > I decided to post a solution to this problem that uses > regular expressions. > > def my_strip(s): >remove_leading = re.sub(r'^\s+','',s) >remove_trailing = > re.sub(r'\s+$','',remove_leading) >new_s = remove_trailing >return new_s

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

2007-10-03 Thread Christopher Spears
I know people might be sick of this thread by now, but I decided to post a solution to this problem that uses regular expressions. #!/usr/bin/env python import string import re def my_strip(s): remove_leading = re.sub(r'^\s+','',s) remove_trailing = re.sub(r'\s+$','',remove_leading)

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

2007-10-03 Thread Ricardo Aráoz
Alan Gauld wrote: > "Ricardo Aráoz" <[EMAIL PROTECTED]> wrote > >>> It still won't work. Strings are immutable, you can't use del >>> to delete a character. Try it. >> It comes straight from Idle (still warm ;-) ). >> Notice s = list(s) > > Shame and embarrassment! I read that code through both t

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

2007-10-03 Thread Alan Gauld
"Ricardo Aráoz" <[EMAIL PROTECTED]> wrote > >It still won't work. Strings are immutable, you can't use del > > to delete a character. Try it. > > It comes straight from Idle (still warm ;-) ). > Notice s = list(s) Shame and embarrassment! I read that code through both times and never noticed the

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

2007-10-03 Thread Ricardo Aráoz
Ian Witham wrote: > > On 10/3/07, *Alan Gauld* <[EMAIL PROTECTED] > > wrote: > > > "Ricardo Aráoz" <[EMAIL PROTECTED] > wrote > > >sorry, forgot a piece of the code : > > > >s = list(s) > >while s[0].isspace() : > >

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

2007-10-03 Thread Ricardo Aráoz
Alan Gauld wrote: > "Ricardo Aráoz" <[EMAIL PROTECTED]> wrote > >> sorry, forgot a piece of the code : >> >> s = list(s) >> while s[0].isspace() : >> while s[-1].isspace() : >> del s[-1] >> del s[0] >> s = ''.join(s) > > It still won't work. Strings are immutable, you can't use de

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

2007-10-03 Thread Ian Witham
On 10/3/07, Alan Gauld <[EMAIL PROTECTED]> wrote: > > > "Ricardo Aráoz" <[EMAIL PROTECTED]> wrote > > >sorry, forgot a piece of the code : > > > >s = list(s) > >while s[0].isspace() : > > while s[-1].isspace() : > > del s[-1] > > del s[0] > > s = ''.join(s) > > It still won't work.

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

2007-10-02 Thread Alan Gauld
"Ricardo Aráoz" <[EMAIL PROTECTED]> wrote >sorry, forgot a piece of the code : > >s = list(s) >while s[0].isspace() : > while s[-1].isspace() : > del s[-1] > del s[0] > s = ''.join(s) It still won't work. Strings are immutable, you can't use del to delete a character. Try it. >>

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

2007-10-02 Thread Ricardo Aráoz
Alan Gauld wrote: > "Ricardo Aráoz" <[EMAIL PROTECTED]> wrote > >> So as not to copy strings many times : >> >> while s[0].isspace() : >>while s[-1].isspace() : >>del s[-1] >>del s[0] > > Sorry, it won;t work. Strings are immutable. > However you can keep a track of the indexes of

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

2007-10-02 Thread Alan Gauld
"Ricardo Aráoz" <[EMAIL PROTECTED]> wrote > > So as not to copy strings many times : > > while s[0].isspace() : >while s[-1].isspace() : >del s[-1] >del s[0] Sorry, it won;t work. Strings are immutable. However you can keep a track of the indexes of the first and last non space c

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

2007-10-02 Thread Ricardo Aráoz
Andrew James wrote: > *headbang* > > I'm an idiot. s=s[:-2] should be s=s[:-1]. > > Andrew James wrote: >> And as soon as I send it again I realise a pretty stupid error. The >> two loops shouldn't be nested. >> >> Andrew James wrote: >>> Helps if I send it to the group... >>> >>> And Kent, I di

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

2007-10-02 Thread Dave Kuhlman
On Tue, Oct 02, 2007 at 06:48:13AM -0400, Kent Johnson wrote: > Christopher Spears wrote: > > I was looking for the source code for the strip > > functions at python.org. I didn't find anything. Do > > you or someone else know where the source code is > > posted? I tried to find python on my wor

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

2007-10-02 Thread Kent Johnson
Andrew James wrote: > Helps if I send it to the group... > > And Kent, I didn't post it originally because I figured the other guy > was still working on his script. Besides, I didn't think it'd be that > far fetched for you to assume I was using something like "while s[0] == > ' ':" Actually

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

2007-10-02 Thread Kent Johnson
Christopher Spears wrote: > I was looking for the source code for the strip > functions at python.org. I didn't find anything. Do > you or someone else know where the source code is > posted? I tried to find python on my workstation, but > I'm not sure where the sys admin installed it. The sour

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

2007-10-02 Thread Alan Gauld
"Christopher Spears" <[EMAIL PROTECTED]> wrote >I was looking for the source code for the strip > functions at python.org. I didn't find anything. Do > you or someone else know where the source code is > posted? I tried to find python on my workstation, but > I'm not sure where the sys admin i

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

2007-10-02 Thread Alan Gauld
"Andrew James" <[EMAIL PROTECTED]> wrote > *headbang* > > I'm an idiot. s=s[:-2] should be s=s[:-1]. > > Andrew James wrote: >> And as soon as I send it again I realise a pretty stupid error. The >> two loops shouldn't be nested. >> >> Andrew James wrote: >>> Helps if I send it to the group... S

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

2007-10-02 Thread Andrew James
*headbang* I'm an idiot. s=s[:-2] should be s=s[:-1]. Andrew James wrote: > And as soon as I send it again I realise a pretty stupid error. The > two loops shouldn't be nested. > > Andrew James wrote: >> Helps if I send it to the group... >> >> And Kent, I didn't post it originally because I fig

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

2007-10-02 Thread Andrew James
And as soon as I send it again I realise a pretty stupid error. The two loops shouldn't be nested. Andrew James wrote: > Helps if I send it to the group... > > And Kent, I didn't post it originally because I figured the other guy > was still working on his script. Besides, I didn't think it'd be

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

2007-10-02 Thread Andrew James
Helps if I send it to the group... And Kent, I didn't post it originally because I figured the other guy was still working on his script. Besides, I didn't think it'd be that far fetched for you to assume I was using something like "while s[0] == ' ':" Andrew James wrote: > I'm always nervous

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

2007-10-01 Thread Christopher Spears
I was looking for the source code for the strip functions at python.org. I didn't find anything. Do you or someone else know where the source code is posted? I tried to find python on my workstation, but I'm not sure where the sys admin installed it. _

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

2007-10-01 Thread Kent Johnson
Andrew James wrote: > 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? H

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

2007-10-01 Thread Alan Gauld
"Andrew James" <[EMAIL PROTECTED]> wrote > string containing a single space ' ' but even using s[-1].isspace() > I > lose punctuation marks. Any idea why that's happening? care to show us what you are doing? >>> ';'.isspace() False So puntuation should not show up as true... Alan G. __

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

2007-10-01 Thread Andrew James
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 t

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

2007-09-30 Thread Bob Nienhuis
BTW, GMail brings up some interesting sponsored links when the subject line has string.strip in it ( :-0) On 9/30/07, Alan Gauld <[EMAIL PROTECTED]> wrote: > > > "wesley chun" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >> > I'm not sure how to proceed. My biggest stumbling >

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

2007-09-30 Thread Alan Gauld
"wesley chun" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >> > 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

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

2007-09-30 Thread wesley chun
> > 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

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

2007-09-29 Thread Alan Gauld
"Christopher Spears" <[EMAIL PROTECTED]> wrote > I'm not sure how to proceed. My biggest stumbling > block is how to detect the leading and trailing > whitespace. Use indexing. Recall str[-1] is the last character str[0] is the first... Try using a while loop. Or maybe two? And strings hav