stripping a string

Jeff Epler jepler at unpythonic.net
Sat Sep 13 20:37:25 EDT 2003


If you want to remove all digits from the string, then use
str.translate, not regular expressions:
    import string
    identity_transformation = string.maketrans('', '')
	
    def remove_digits(s):
	return s.translate(identity_transformation, string.digits)

>>> s = 'ANL LN32'
>>> remove_digits(s)	
'ANL LN'

If you want to remove digits from the end of the string, then use
str.strip(), not a regular expression:
>>> s.rstrip(string.digits)
'ANL LN'

Jeff





More information about the Python-list mailing list