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

Christopher Spears cspears2002 at yahoo.com
Thu Oct 4 02:08:53 CEST 2007


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)
    new_s = remove_trailing
    return new_s

test = raw_input("Enter a string with leading and
trailing whitespace: ")
#print test
new_s = my_strip(test)
print new_s



More information about the Tutor mailing list