按单词反转字符串:保持每个单词内的字母顺序不变,只是将单词的顺序反转。例如:
原来的字符串:
That is a boy
反转后变为:
boy a is That
def reverse_word(s): s1 = s.split(' ') i, j = 0, len(s1)-1 while i < j: s1[i], s1[j] = s1[j], s1[i] i += 1 j -= 1 return ' '.join(s1) if __name__=="__main__": s = 'That is a boy' print(reverse_word(s))