1

I’d like to eliminate numbers in a string in Python.

str = "aaaa22222111111kkkkk"

I want this to be "aaaakkkkk".

I use re.sub to replace, but it doesn't work:

str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)

Maybe, this replaces a string which only contains numbers with "".

How should I do with this?

Alex Riley
  • 152,205
  • 43
  • 245
  • 225
user3119018
  • 199
  • 1
  • 4
  • 12

1 Answers1

2

your regex is wrong:

re.sub(r'[0-9]',"",str)

should work:

>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'
WeaselFox
  • 7,100
  • 7
  • 42
  • 73