code: def _remove_whitespace(text):
          
          # while True:
          #     old_text = text
          #     text = text.replace('  ', ' ')
          #     if text == old_text:
          #         return text
          non_spaces = re.finditer(r'[^ ]', text)
  
          if not non_spaces:
              return text
  
          first_non_space = non_spaces.next()
          first_non_space = first_non_space.start()
  
          last_non_space = None
          for item in non_spaces:
              last_non_space = item
  
          if not last_non_space:
              return text[first_non_space:]
          else:
              last_non_space = last_non_space.end()
              return text[first_non_space:last_non_space]
docs: Remove excess whitespace from the ends of a given input string.