import re def extra(line): # Remove space, colon, semicolon string = re.split('[ :;]', line) # Get the indices of the spaces space, i = [], 0 while i < len(string): if string[i] == '': space.insert(0, i) i += 1 # Delete the spaces for i in space: del string[i] # Add a space between every token newline = '' for char in string: newline = newline + ' ' + char string = newline[1:] # Strip the tokens apart and display them for token in string.split(): print token print 'END' line1 = 'the first; line of input to be parsed' line2 = 'blah!@#$% blah%^@# blah;:*()905' line3 = 'split kicks a*#!!!' extra(line1) extra(line2) extra(line3)