stripping a string

Tim Williams timothy.williams at nvl.army.mil
Mon Sep 15 11:45:31 EDT 2003


Jeff Epler <jepler at unpythonic.net> wrote in message news:<mailman.1063499902.1786.python-list at python.org>...
> 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

I don't understand. What's wrong with

>>> import re
>>> s = 'ANL LN32'
>>> s=re.sub('[0-9]', '', s)
>>> print s
ANL LN




More information about the Python-list mailing list