Có nhiều cách. Filter non-digit character dùng for ... if. Ví dụ:
>>> s = "H3ll0 P30P13" >>> ''.join(i for i in s if i.isdigit()) '303013'
Cũng dùng filter và lambda function filter ký tự. Ví dụ:
>>> s = "H3ll0 P30P13" >>> filter(lambda x: x.isdigit(), s) '303013'
Dù overkill cho task đơn giản, cũng dùng regex. Ký tự \D (non-digit) replace bằng empty string. Ví dụ:
>>> import re
>>> s = "H3ll0 P30P13"
>>> re.sub("\D", "", s)
'303013'