-4

Can someone please hint my mistake. Im trying to remove numbers from the string. Findall succefully finds the number but replace does not replace them with empty string. here is the code:

import re
x = 'John2345 can5 code with python3'
extra = re.findall('\d{1,9}', x)
x.replace(extra, '')
x

and here is the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-334-bfc161d88f65> in <module>()
      2 x = 'ererf ergg5 erg 545 eeg'
      3 extra = re.findall('\d{1,9}', x)
----> 4 x.replace(extra, '')
      5 x

TypeError: Can't convert 'list' object to str implicitly

Cheers, Sia

Sia
  • 1
  • 2

2 Answers2

0

Use re.sub()

re.sub(r'\d+' ,'', x)
bhansa
  • 7,282
  • 3
  • 30
  • 55
0

x is a string, extra is a list, right? The best hint is in the error message you recieved:

TypeError: Can't convert 'list' object to str implicitly

What python actually tells you, in human language is "I can't get your list object as argument here!! give me a string!!!"

barshopen
  • 1,190
  • 2
  • 15
  • 28