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

Alan Gauld alan.gauld at btinternet.com
Thu Oct 4 09:13:51 CEST 2007


"Christopher Spears" <cspears2002 at yahoo.com> 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

You can replace the last two lines with 

return remove_trailing

There is no need for new_s
(In fact you could just return the last sub result 
and dispense with remove_training too!)

Alan G.



More information about the Tutor mailing list