开发者社区 问答 正文

如何将字符串添加到空变量以构成长字?

我想把列表中的所有对象放在一起,我的想法是创建一个没有任何内容的变量,并使一个for循环将字符串添加到变量...但它不起作用

sounds = ["super", "cali", "fragil", "istic", "expi", "ali", "docious"]
x=""
for s in sounds:

x+s

print(x)
我期望x最后成为“supercalifragilisticexpialidocious”。

展开
收起
一码平川MACHEL 2019-01-22 11:51:44 1552 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 你可以使用join:

    sounds = ["super", "cali", "fragil", "istic", "expi", "ali", "docious"]

    x = "".join(sounds)

    print(x)
    至于你的解决方案,你错过了一个=:

    sounds = ["super", "cali", "fragil", "istic", "expi", "ali", "docious"]
    x=""
    for s in sounds:

    x += s

    print(x)
    你得到同样的事件:

    supercalifragilisticexpialidocious

    2019-07-17 23:26:14 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答地址:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等