s = '    Mary    had    a little lamb  '
s
len0 = len(s)
s = s.rstrip()
len1 = len(s)
s = s.lstrip()
s = ' '.join(s.split())
print(s)
s = s.rjust(len1).ljust(len0)
s
Without regex reduce multiple spaces in a string to one in Python