【发布时间】:2019-01-01 01:52:33
【问题描述】:
我有一个字符串,我将其拆分为一个列表,并且能够以相反的顺序成功地正确打印出来。但是,当我将列表重新加入字符串时,它只是作为列表保留。我的加入功能哪里出错了?
我的代码:
sample_string = 'Hello Dragon and Snakie'
words = sample_string.split(" ")
reordered = str(words[::-1])
final = "".join(reordered)
print(final)
预期:蛇和龙你好
实际:['Snakie', 'and', 'Dragon', 'Hello']
谢谢
【问题讨论】:
-
你的输出实际上是一个字符串——一个带括号的字符串,所以它看起来像一个列表。
reordered是一个字符串,而不是一个列表。只需取出str函数并为您的join方法添加一个空格来分隔单词。
标签: python python-3.x