import sys BEFORE = 1; INSIDE = 2; AFTER = 3 # states state = BEFORE while True: c = sys.stdin.read(1) if '' == c: break # end of file if (state == BEFORE): if (c != ' '): state = INSIDE sys.stdout.write(c) elif (state == INSIDE): if c == ' ' : state = AFTER else: if c == '\n': state = BEFORE sys.stdout.write(c) elif (state == AFTER): if c == '\n': state = BEFORE sys.stdout.write(c)