def spin_words(a):
lenth = len(a)
# print(len(a))
b = []
c=[]
for i in range(lenth):
if a[i] == " ":
b.append(i) # 定位出空格所在位置前一个字符位置
b.append(lenth)
b=b[::-1]
# print(b)
if len(b)==1:
c=a[::-1]
else:
for j in range(len(b)):
if b[j] == b[-1]:
temp = a[:b[j] + 1][::-1] # 包含右空格
temp = temp +" "
elif b[j] == b[-1]:
temp = a[b[j + 1] + 1:b[j] + 1][::-1] # 前闭后开,不包含左空格,包含右空格
else:
temp = a[b[j + 1]:b[j] + 1][::-1]
c.append(temp)
c="".join(c)
return c
a='htiw ekil tub nehw lliw sdrow eht ni eno lliw tsuJ naht ro erom emas tsuJ erom ylno lla taht ro'
print(a)
print(spin_words(a))
htiw ekil tub nehw lliw sdrow eht ni eno lliw tsuJ naht ro erom emas tsuJ erom ylno lla taht ro
or that all only more Just same more or than Just will one in the words will when but like with