正确代码:
1.用split 再 join 切片后倒序的字符串
str = "hello world hello python"
str_1 = str.split()
str_2 = " ".join(str_1[::-1])
print(str_2)
2.用split 再reverse ,最后join拼接
str = "hello world hello python"
str_1 = str.split()
str_1.reverse()
str_2 = " ".join(str_1)
print(str_2)
python里面的切片[::-1] 和 reverse()函数区别:
切片[::-1] 有返回值, reverse()函数没有返回值
有返回值的可以 return、赋值。没有的不能 return、赋值。