from itertools import groupby
s = '    Mary    had    a little lamb  '
res = ''.join(' ' if is_space else ''.join(chars) for is_space, chars in
    groupby(s, str.isspace))
Without regex reduce multiple spaces in a string to one in Python