def nospecial(text): import re text = re.sub("[^a-zA-Z0-9]+", "",text) return text
string = "abc123" # Method 1 ''.join(char for char in string if char.isdigit()) #Method 2 import re re.sub("[^0-9]", "", string)