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
  • 169,130
  • 45
  • 262
  • 238
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,220
  • 8
  • 44
  • 75