def my_handle(self):sentence = ' hello apple '' '.join(x.strip() for x in sentence.split())#hello apple''.join(x.strip() for x in sentence.split())#helloapple
# Import regular expression moduleimport re
# Initialize stringa = " foo bar "
# First replace any number of spaces with a single spacea = re.sub(' +', ' ', a)
# Then strip any leading and trailing spaces.a = a.strip()
# Show resultsprint(a)
test_string = ' test a s test 'string_list = [s.strip() for s in str(test_string).split()]final_string = ' '.join(string_array)# final_string: 'test a s test'