Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

remove numbers from string using python

hello I have a question:
How can I remove numbers from string, I know that the fasted and best way is to use translate

'hello467'.translate(None, '0123456789')

will return hello, but what if I want to remove only numbers that are not attached to a sting for example: 'the 1rst of every month I spend 10 dollars' should be 'the 1rst of every month I spend dollars' and not 'the rst of every month I spend dollars'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import re

s = "the 1rst of every month I spend 10 dollars"
 
result = re.sub(r"\b\d+\b", '', s)
result = re.sub("  ", ' ', result)

should give:

the 1rst of every month I spend dollars
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading