假设你有这个: Laugh = 'hehe' 这个例子的实用性并不重要,但如果你想通过以下方式将笑放入一个列表中:laugh = list(laugh) 并且你确实大笑[1] = '一种'。你会如何把笑变成“哈哈”?
2 回答

明月笑刀无情
TA贡献1828条经验 获得超4个赞
通常,要将列表转换为字符串,请使用''.join:
laugh = 'hehe'
laugh = list(laugh)
laugh[1] = 'a'
laugh = ''.join(laugh)
这比使用for-loop等其他方法要好:
new_laugh = ''
for c in laugh:
new_laugh += c

郎朗坤
TA贡献1921条经验 获得超9个赞
最简单的方法(仅使用+运算符 - 如果您想要一个用户定义的函数来完成任务)是将列表的组件添加到一个字符串中:
def string_to_list(L):
S = ""
for i in L:
S = S + str(L)
return S
string_to_list(laugh)
您还可以使用该join()函数来执行相同的操作:
Laugh = ''.join(laugh)
添加回答
举报
0/150
提交
取消