Piggybacking on the idea of ββjsbueno using str.translate and then split:
import string allchars = ''.join(chr(i) for i in range(32,256)) digExtractTrans = string.maketrans(allchars, ''.join(ch if ch.isdigit() else ' ' for ch in allchars)) alpExtractTrans = string.maketrans(allchars, ''.join(ch if ch.isalpha() else ' ' for ch in allchars)) data = "5people10cars" numbers = data.translate(digExtractTrans).split() names = data.translate(alpExtractTrans).split()
You only need to create the translation tables once, and then translate and split as often as you want.
source share