import re #a string str = '63__foo,,bar,_mango_,apple' #split string into chunks chunks = re.split('[_,][_,]',str) #print chunks print(chunks)