Removing all non-numeric characters from string in Python

grizzley picture grizzley · Aug 8, 2009 · Viewed 164k times · Source

How do we remove all non-numeric characters from a string in Python?

Answer

Ned Batchelder picture Ned Batchelder · Aug 8, 2009
>>> import re
>>> re.sub("[^0-9]", "", "sdkjh987978asd098as0980a98sd")
'987978098098098'