def reverse(str_list, start, end): while start < end: str_list[start], str_list[end] = str_list[end], str_list[start] start += 1 end -= 1 sentence = ' I love Jinchenn' str_list = list(sentence) i = 0 while i < len(str_list): if str_list[i] != ' ': start = i end = i + 1 while (end < len(str_list)) and str_list[end] != ' ': end += 1 i = end reverse(str_list,start,end-1) else: i += 1 str_list.reverse() print(''.join(str_list))