stripping a string

Jeff Epler jepler at
Sun Sep 14 02:37:25 CEST 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)	

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


More information about the Python-list mailing list